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
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: if index == r: for j in range(lowerCamelCase_ ): print(data[j] , end=' ' ) print(' ' ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location lowerCAmelCase__ : List[str] = arr[i] combination_util(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , index + 1 , lowerCamelCase_ , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: lowerCAmelCase__ : List[str] = [0] * r # Print all combination using temporary array 'data[]' combination_util(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , 0 , lowerCamelCase_ , 0 ) if __name__ == "__main__": # Driver code to check the function above lowerCamelCase__ = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
358
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class A__ ( __magic_name__ ): lowercase = (DPMSolverSDEScheduler,) lowercase = 10 def _lowerCamelCase ( self : Optional[int] , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = { 'num_train_timesteps': 1_100, 'beta_start': 0.0_0_0_1, 'beta_end': 0.0_2, 'beta_schedule': 'linear', 'noise_sampler_seed': 0, } config.update(**a ) return config def _lowerCamelCase ( self : Tuple ): '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=a ) def _lowerCamelCase ( self : int ): '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=a , beta_end=a ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=a ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = self.scheduler_classes[0] lowerCAmelCase__ : Tuple = self.get_scheduler_config() lowerCAmelCase__ : List[Any] = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Dict = self.dummy_model() lowerCAmelCase__ : int = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : int = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : List[Any] = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : int = scheduler.step(a , a , a ) lowerCAmelCase__ : Any = output.prev_sample lowerCAmelCase__ : List[Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Optional[int] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_7_8_2_1_0_4_4_9_2_1_8_7_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_7_8_7_0_5_9_6_4_5_6_5_2_7_7 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_2_1_1_1_8_1_6_4_0_6 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_6_8_9_2_2_9_9_6_5_2 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.scheduler_classes[0] lowerCAmelCase__ : List[str] = self.get_scheduler_config(prediction_type='v_prediction' ) lowerCAmelCase__ : Any = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Optional[int] = self.dummy_model() lowerCAmelCase__ : Union[str, Any] = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : Any = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : str = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : Dict = scheduler.step(a , a , a ) lowerCAmelCase__ : Tuple = output.prev_sample lowerCAmelCase__ : int = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Union[str, Any] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_2_4.7_7_1_4_9_2_0_0_4_3_9_4_5_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_2_2_6_2_8_9_0_1_4_8_1_6_2_8_4 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_2_8.1_6_6_3_3_6_0_5_9_5_7_0_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_6_8_8_3_2_6_0_0_1_1_6_7_2_9_7 ) < 1E-3 else: assert abs(result_sum.item() - 1_1_9.8_4_8_7_5_4_8_8_2_8_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.1_5_6_0_5_3_0_6_6_2_5_3_6_6_2_1 ) < 1E-3 def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.scheduler_classes[0] lowerCAmelCase__ : Optional[int] = self.get_scheduler_config() lowerCAmelCase__ : int = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : Tuple = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: lowerCAmelCase__ : Dict = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : Optional[int] = model(a , a ) lowerCAmelCase__ : Tuple = scheduler.step(a , a , a ) lowerCAmelCase__ : Dict = output.prev_sample lowerCAmelCase__ : Union[str, Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Dict = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_6_9_5_7_3_9_7_4_6_0_9_3_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_8_0_5_9_3_4_6_0_7_9_8_2_6_3_5 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_3_6_3_7_6_9_5_3_1_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_8_3_8_2_4_1_5_7_7_1 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.scheduler_classes[0] lowerCAmelCase__ : Any = self.get_scheduler_config() lowerCAmelCase__ : Any = scheduler_class(**a , use_karras_sigmas=a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : str = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma lowerCAmelCase__ : str = sample.to(a ) for t in scheduler.timesteps: lowerCAmelCase__ : Any = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : int = model(a , a ) lowerCAmelCase__ : Union[str, Any] = scheduler.step(a , a , a ) lowerCAmelCase__ : Union[str, Any] = output.prev_sample lowerCAmelCase__ : Optional[int] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Any = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_7_6.6_6_9_7_4_1_3_5_7_4_2_1_8_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_7.6_3_6_5_3_5_6_4_4_5_3_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 else: assert abs(result_sum.item() - 1_7_0.3_1_3_5_2_2_3_3_8_8_6_7_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2
307
0
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass lowerCamelCase__ = (3, 9, -11, 0, 7, 5, 1, -1) lowerCamelCase__ = (4, 6, 2, 0, 8, 10, 3, -2) @dataclass class A__ : lowercase = 42 lowercase = 42 class A__ : def __init__( self : Dict , a : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : int = None for i in sorted(__lowercase , reverse=__lowercase ): lowerCAmelCase__ : List[Any] = Node(__lowercase , self.head ) def __iter__( self : str ): '''simple docstring''' lowerCAmelCase__ : Dict = self.head while node: yield node.data lowerCAmelCase__ : Any = node.next_node def __len__( self : int ): '''simple docstring''' return sum(1 for _ in self ) def __str__( self : Optional[Any] ): '''simple docstring''' return " -> ".join([str(__lowercase ) for node in self] ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: return SortedLinkedList(list(SCREAMING_SNAKE_CASE_ ) + list(SCREAMING_SNAKE_CASE_ ) ) if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ = SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
359
import os import string import sys lowerCamelCase__ = 1 << 8 lowerCamelCase__ = { """tab""": ord("""\t"""), """newline""": ord("""\r"""), """esc""": 27, """up""": 65 + ARROW_KEY_FLAG, """down""": 66 + ARROW_KEY_FLAG, """right""": 67 + ARROW_KEY_FLAG, """left""": 68 + ARROW_KEY_FLAG, """mod_int""": 91, """undefined""": sys.maxsize, """interrupt""": 3, """insert""": 50, """delete""": 51, """pg_up""": 53, """pg_down""": 54, } lowerCamelCase__ = KEYMAP["""up"""] lowerCamelCase__ = KEYMAP["""left"""] if sys.platform == "win32": lowerCamelCase__ = [] lowerCamelCase__ = { b"""\xe0H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\x00H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\xe0P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\x00P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\xe0M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\x00M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\xe0K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, b"""\x00K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, } for i in range(10): lowerCamelCase__ = ord(str(i)) def lowerCAmelCase__ ( ) -> Dict: if os.name == "nt": import msvcrt lowerCAmelCase__ : Dict = 'mbcs' # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(SCREAMING_SNAKE_CASE_ ) == 0: # Read the keystroke lowerCAmelCase__ : Optional[Any] = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): lowerCAmelCase__ : Dict = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: lowerCAmelCase__ : Dict = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP['mod_int'] ) ) WIN_CH_BUFFER.append(SCREAMING_SNAKE_CASE_ ) if ord(SCREAMING_SNAKE_CASE_ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) lowerCAmelCase__ : Dict = chr(KEYMAP['esc'] ) except KeyError: lowerCAmelCase__ : Dict = cha[1] else: lowerCAmelCase__ : List[Any] = ch.decode(SCREAMING_SNAKE_CASE_ ) else: lowerCAmelCase__ : Tuple = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty lowerCAmelCase__ : Tuple = sys.stdin.fileno() lowerCAmelCase__ : Any = termios.tcgetattr(SCREAMING_SNAKE_CASE_ ) try: tty.setraw(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = sys.stdin.read(1 ) finally: termios.tcsetattr(SCREAMING_SNAKE_CASE_ , termios.TCSADRAIN , SCREAMING_SNAKE_CASE_ ) return ch def lowerCAmelCase__ ( ) -> Union[str, Any]: lowerCAmelCase__ : Any = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["esc"]: lowerCAmelCase__ : Union[str, Any] = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["mod_int"]: lowerCAmelCase__ : str = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(SCREAMING_SNAKE_CASE_ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(SCREAMING_SNAKE_CASE_ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
307
0
"""simple docstring""" def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> int: if len(__lowerCamelCase ) <= 1: return [tuple(__lowerCamelCase )] lowerCAmelCase__ : str = [] def generate(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , __lowerCamelCase ) for i in range(k - 1 ): if k % 2 == 0: # k is even lowerCAmelCase__ : Any = arr[k - 1], arr[i] else: # k is odd lowerCAmelCase__ : Dict = arr[k - 1], arr[0] generate(k - 1 , __lowerCamelCase ) generate(len(__lowerCamelCase ) , __lowerCamelCase ) return res if __name__ == "__main__": lowerCamelCase__ = input("""Enter numbers separated by a comma:\n""").strip() lowerCamelCase__ = [int(item) for item in user_input.split(""",""")] print(heaps(arr))
360
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: return "".join([hex(SCREAMING_SNAKE_CASE_ )[2:].zfill(2 ).upper() for byte in list(SCREAMING_SNAKE_CASE_ )] ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> bytes: # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(SCREAMING_SNAKE_CASE_ ) % 2) != 0: raise ValueError( 'Base16 encoded data is invalid:\nData does not have an even number of hex digits.' ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(SCREAMING_SNAKE_CASE_ ) <= set('0123456789ABCDEF' ): raise ValueError( 'Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.' ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
307
0
from math import pi, sqrt, tan def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> float: if side_length < 0: raise ValueError('surface_area_cube() only accepts non-negative values' ) return 6 * side_length**2 def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if length < 0 or breadth < 0 or height < 0: raise ValueError('surface_area_cuboid() only accepts non-negative values' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> float: if radius < 0: raise ValueError('surface_area_sphere() only accepts non-negative values' ) return 4 * pi * radius**2 def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> float: if radius < 0: raise ValueError('surface_area_hemisphere() only accepts non-negative values' ) return 3 * pi * radius**2 def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if radius < 0 or height < 0: raise ValueError('surface_area_cone() only accepts non-negative values' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( 'surface_area_conical_frustum() only accepts non-negative values' ) lowerCAmelCase__ : str = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if radius < 0 or height < 0: raise ValueError('surface_area_cylinder() only accepts non-negative values' ) return 2 * pi * radius * (height + radius) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if torus_radius < 0 or tube_radius < 0: raise ValueError('surface_area_torus() only accepts non-negative values' ) if torus_radius < tube_radius: raise ValueError( 'surface_area_torus() does not support spindle or self intersecting tori' ) return 4 * pow(lowerCAmelCase__ , 2 ) * torus_radius * tube_radius def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if length < 0 or width < 0: raise ValueError('area_rectangle() only accepts non-negative values' ) return length * width def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> float: if side_length < 0: raise ValueError('area_square() only accepts non-negative values' ) return side_length**2 def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if base < 0 or height < 0: raise ValueError('area_triangle() only accepts non-negative values' ) return (base * height) / 2 def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('area_triangle_three_sides() only accepts non-negative values' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('Given three sides do not form a triangle' ) lowerCAmelCase__ : Dict = (sidea + sidea + sidea) / 2 lowerCAmelCase__ : List[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if base < 0 or height < 0: raise ValueError('area_parallelogram() only accepts non-negative values' ) return base * height def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if basea < 0 or basea < 0 or height < 0: raise ValueError('area_trapezium() only accepts non-negative values' ) return 1 / 2 * (basea + basea) * height def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> float: if radius < 0: raise ValueError('area_circle() only accepts non-negative values' ) return pi * radius**2 def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if radius_x < 0 or radius_y < 0: raise ValueError('area_ellipse() only accepts non-negative values' ) return pi * radius_x * radius_y def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if diagonal_a < 0 or diagonal_a < 0: raise ValueError('area_rhombus() only accepts non-negative values' ) return 1 / 2 * diagonal_a * diagonal_a def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) or sides < 3: raise ValueError( 'area_reg_polygon() only accepts integers greater than or \\nequal to three as number of sides' ) elif length < 0: raise ValueError( 'area_reg_polygon() only accepts non-negative values as \\nlength of a side' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print("""[DEMO] Areas of various geometric shapes: \n""") print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print("""\nSurface Areas of various geometric shapes: \n""") print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
361
from __future__ import annotations def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[list[int]]: lowerCAmelCase__ : list[list[int]] = [] create_all_state(1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , [] , SCREAMING_SNAKE_CASE_ ) return result def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) -> None: if level == 0: total_list.append(current_list[:] ) return for i in range(SCREAMING_SNAKE_CASE_ , total_number - level + 2 ): current_list.append(SCREAMING_SNAKE_CASE_ ) create_all_state(i + 1 , SCREAMING_SNAKE_CASE_ , level - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) current_list.pop() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> None: for i in total_list: print(*SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCamelCase__ = 4 lowerCamelCase__ = 2 lowerCamelCase__ = generate_all_combinations(n, k) print_all_state(total_list)
307
0
import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version lowerCamelCase__ = get_logger(__name__) class A__ : lowercase = 'dummy_data' lowercase = 'datasets' lowercase = False def __init__( self : int , a : List[str] , a : Optional[int] , a : str , a : int = None , a : List[str] = False , a : List[Any] = True , a : List[Any] = None , ): '''simple docstring''' lowerCAmelCase__ : str = 0 lowerCAmelCase__ : Union[str, Any] = dataset_name lowerCAmelCase__ : List[Any] = cache_dir lowerCAmelCase__ : str = use_local_dummy_data lowerCAmelCase__ : List[Any] = config # download_callbacks take a single url as input lowerCAmelCase__ : List[Callable] = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root lowerCAmelCase__ : List[Any] = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general lowerCAmelCase__ : Optional[Any] = str(a ) # to be downloaded lowerCAmelCase__ : Optional[int] = None lowerCAmelCase__ : Any = None @property def _lowerCamelCase ( self : Any ): '''simple docstring''' if self._dummy_file is None: lowerCAmelCase__ : List[str] = self.download_dummy_data() return self._dummy_file @property def _lowerCamelCase ( self : Dict ): '''simple docstring''' if self.config is not None: # structure is dummy / config_name / version_name return os.path.join('dummy' , self.config.name , self.version_name ) # structure is dummy / version_name return os.path.join('dummy' , self.version_name ) @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return os.path.join(self.dummy_data_folder , 'dummy_data.zip' ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) lowerCAmelCase__ : int = cached_path( a , cache_dir=self.cache_dir , extract_compressed_file=a , force_extract=a ) return os.path.join(a , self.dummy_file_name ) @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return os.path.join(self.datasets_scripts_dir , self.dataset_name , self.dummy_zip_file ) @property def _lowerCamelCase ( self : Dict ): '''simple docstring''' if self._bucket_url is None: lowerCAmelCase__ : Tuple = hf_github_url(self.dataset_name , self.dummy_zip_file.replace(os.sep , '/' ) ) return self._bucket_url @property def _lowerCamelCase ( self : Tuple ): '''simple docstring''' if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep , '/' ).split('/' )[:-1] ) def _lowerCamelCase ( self : Any , a : List[Any] , *a : List[str] ): '''simple docstring''' if self.load_existing_dummy_data: # dummy data is downloaded and tested lowerCAmelCase__ : int = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned lowerCAmelCase__ : List[str] = self.dummy_file_name # special case when data_url is a dict if isinstance(a , a ): return self.create_dummy_data_dict(a , a ) elif isinstance(a , (list, tuple) ): return self.create_dummy_data_list(a , a ) else: return self.create_dummy_data_single(a , a ) def _lowerCamelCase ( self : str , a : Dict , *a : int ): '''simple docstring''' return self.download_and_extract(a ) def _lowerCamelCase ( self : List[Any] , a : str , a : Tuple ): '''simple docstring''' return self.download_and_extract(a ) def _lowerCamelCase ( self : List[str] , a : Optional[int] , *a : List[Any] , **a : Optional[Any] ): '''simple docstring''' return path def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return {} def _lowerCamelCase ( self : List[str] , a : Any , a : Any ): '''simple docstring''' lowerCAmelCase__ : List[str] = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(a , a ): for single_url in single_urls: download_callback(a ) else: lowerCAmelCase__ : Dict = single_urls download_callback(a ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(a , a ): lowerCAmelCase__ : Optional[Any] = [os.path.join(a , urllib.parse.quote_plus(Path(a ).name ) ) for x in single_urls] else: lowerCAmelCase__ : Tuple = single_urls lowerCAmelCase__ : Dict = os.path.join(a , urllib.parse.quote_plus(Path(a ).name ) ) lowerCAmelCase__ : List[Any] = value # make sure that values are unique if all(isinstance(a , a ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique lowerCAmelCase__ : Dict = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def _lowerCamelCase ( self : Union[str, Any] , a : Union[str, Any] , a : Dict ): '''simple docstring''' lowerCAmelCase__ : int = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one lowerCAmelCase__ : str = all(bool(re.findall('[0-9]{3,}-of-[0-9]{3,}' , a ) ) for url in data_url ) lowerCAmelCase__ : str = all( url.startswith('https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed' ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): lowerCAmelCase__ : Optional[Any] = [data_url[0]] * len(a ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(a ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus lowerCAmelCase__ : Dict = os.path.join(a , urllib.parse.quote_plus(single_url.split('/' )[-1] ) ) dummy_data_list.append(a ) return dummy_data_list def _lowerCamelCase ( self : Optional[Any] , a : Union[str, Any] , a : Union[str, Any] ): '''simple docstring''' for download_callback in self.download_callbacks: download_callback(a ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus lowerCAmelCase__ : Dict = os.path.join(a , urllib.parse.quote_plus(data_url.split('/' )[-1] ) ) if os.path.exists(a ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def _lowerCamelCase ( self : str ): '''simple docstring''' pass def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : str , a : List[Any] ): '''simple docstring''' def _iter_archive_members(a : str ): # this preserves the order of the members inside the ZIP archive lowerCAmelCase__ : List[Any] = Path(self.dummy_file ).parent lowerCAmelCase__ : Tuple = path.relative_to(a ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: lowerCAmelCase__ : Union[str, Any] = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(a ) lowerCAmelCase__ : Any = Path(a ) lowerCAmelCase__ : Optional[int] = _iter_archive_members(a ) if self.use_local_dummy_data else path.rglob('*' ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith(('.', '__') ): yield file_path.relative_to(a ).as_posix(), file_path.open('rb' ) def _lowerCamelCase ( self : Union[str, Any] , a : List[str] ): '''simple docstring''' if not isinstance(a , a ): lowerCAmelCase__ : List[Any] = [paths] for path in paths: if os.path.isfile(a ): if os.path.basename(a ).startswith(('.', '__') ): return yield path else: for dirpath, dirnames, filenames in os.walk(a ): if os.path.basename(a ).startswith(('.', '__') ): continue dirnames.sort() for filename in sorted(a ): if filename.startswith(('.', '__') ): continue yield os.path.join(a , a )
362
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class A__ ( unittest.TestCase ): @parameterized.expand([(None,), ('foo.json',)] ) def _lowerCamelCase ( self : Dict , a : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a , config_name=a ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(a , config_name=a ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 50 ) self.assertEqual(loaded_config.max_length , 20 ) self.assertEqual(loaded_config.max_time , a ) def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Dict = AutoConfig.from_pretrained('gpt2' ) lowerCAmelCase__ : Any = GenerationConfig.from_model_config(a ) lowerCAmelCase__ : Any = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(a , a ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : Dict = { 'max_new_tokens': 1_024, 'foo': 'bar', } lowerCAmelCase__ : List[Any] = copy.deepcopy(a ) lowerCAmelCase__ : Dict = generation_config.update(**a ) # update_kwargs was not modified (no side effects) self.assertEqual(a , a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_024 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(a , {'foo': 'bar'} ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : List[Any] = 'bar' with tempfile.TemporaryDirectory('test-generation-config' ) as tmp_dir: generation_config.save_pretrained(a ) lowerCAmelCase__ : List[Any] = GenerationConfig.from_pretrained(a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , 'bar' ) lowerCAmelCase__ : int = GenerationConfig.from_model_config(a ) assert not hasattr(a , 'foo' ) # no new kwargs should be initialized if from config def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , a ) self.assertEqual(default_config.num_beams , 1 ) lowerCAmelCase__ : List[Any] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , a ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(a , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class A__ ( unittest.TestCase ): @classmethod def _lowerCamelCase ( cls : int ): '''simple docstring''' lowerCAmelCase__ : List[str] = TOKEN HfFolder.save_token(a ) @classmethod def _lowerCamelCase ( cls : Optional[int] ): '''simple docstring''' try: delete_repo(token=cls._token , repo_id='test-generation-config' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-generation-config-org' ) except HTTPError: pass def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('test-generation-config' , use_auth_token=self._token ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='test-generation-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='test-generation-config' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('valid_org/test-generation-config-org' , use_auth_token=self._token ) lowerCAmelCase__ : Dict = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-generation-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='valid_org/test-generation-config-org' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : List[str] = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) )
307
0
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCamelCase__ = { """configuration_vivit""": ["""VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """VivitConfig"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""VivitImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """VivitModel""", """VivitPreTrainedModel""", """VivitForVideoClassification""", ] if TYPE_CHECKING: from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_vivit import VivitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vivit import ( VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST, VivitForVideoClassification, VivitModel, VivitPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
363
import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import ( DiffusionPipeline, UnCLIPImageVariationPipeline, UnCLIPScheduler, UNetaDConditionModel, UNetaDModel, ) from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, load_image, require_torch_gpu, skip_mps from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = UnCLIPImageVariationPipeline lowercase = IMAGE_VARIATION_PARAMS - {'height', 'width', 'guidance_scale'} lowercase = IMAGE_VARIATION_BATCH_PARAMS lowercase = [ 'generator', 'return_dict', 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] lowercase = False @property def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : int ): '''simple docstring''' return self.time_input_dim @property def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return self.time_input_dim * 4 @property def _lowerCamelCase ( self : Any ): '''simple docstring''' return 100 @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) return tokenizer @property def _lowerCamelCase ( self : Any ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) return CLIPTextModelWithProjection(a ) @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : List[Any] = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) return CLIPVisionModelWithProjection(a ) @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Union[str, Any] = { 'clip_embeddings_dim': self.text_embedder_hidden_size, 'time_embed_dim': self.time_embed_dim, 'cross_attention_dim': self.cross_attention_dim, } lowerCAmelCase__ : Optional[Any] = UnCLIPTextProjModel(**a ) return model @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : str = { 'sample_size': 32, # RGB in channels 'in_channels': 3, # Out channels is double in channels because predicts mean and variance 'out_channels': 6, 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': 'identity', } lowerCAmelCase__ : str = UNetaDConditionModel(**a ) return model @property def _lowerCamelCase ( self : str ): '''simple docstring''' return { "sample_size": 64, "layers_per_block": 1, "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"), "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"), "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "in_channels": 6, "out_channels": 3, } @property def _lowerCamelCase ( self : str ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Any = UNetaDModel(**self.dummy_super_res_kwargs ) return model @property def _lowerCamelCase ( self : int ): '''simple docstring''' torch.manual_seed(1 ) lowerCAmelCase__ : List[str] = UNetaDModel(**self.dummy_super_res_kwargs ) return model def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.dummy_decoder lowerCAmelCase__ : Optional[int] = self.dummy_text_proj lowerCAmelCase__ : Any = self.dummy_text_encoder lowerCAmelCase__ : Any = self.dummy_tokenizer lowerCAmelCase__ : Any = self.dummy_super_res_first lowerCAmelCase__ : Optional[int] = self.dummy_super_res_last lowerCAmelCase__ : Dict = UnCLIPScheduler( variance_type='learned_range' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = UnCLIPScheduler( variance_type='fixed_small_log' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = CLIPImageProcessor(crop_size=32 , size=32 ) lowerCAmelCase__ : Optional[int] = self.dummy_image_encoder return { "decoder": decoder, "text_encoder": text_encoder, "tokenizer": tokenizer, "text_proj": text_proj, "feature_extractor": feature_extractor, "image_encoder": image_encoder, "super_res_first": super_res_first, "super_res_last": super_res_last, "decoder_scheduler": decoder_scheduler, "super_res_scheduler": super_res_scheduler, } def _lowerCamelCase ( self : Any , a : Dict , a : List[str]=0 , a : List[str]=True ): '''simple docstring''' lowerCAmelCase__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(a ) ).to(a ) if str(a ).startswith('mps' ): lowerCAmelCase__ : Optional[int] = torch.manual_seed(a ) else: lowerCAmelCase__ : str = torch.Generator(device=a ).manual_seed(a ) if pil_image: lowerCAmelCase__ : Optional[int] = input_image * 0.5 + 0.5 lowerCAmelCase__ : Dict = input_image.clamp(0 , 1 ) lowerCAmelCase__ : List[Any] = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() lowerCAmelCase__ : Union[str, Any] = DiffusionPipeline.numpy_to_pil(a )[0] return { "image": input_image, "generator": generator, "decoder_num_inference_steps": 2, "super_res_num_inference_steps": 2, "output_type": "np", } def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = 'cpu' lowerCAmelCase__ : Any = self.get_dummy_components() lowerCAmelCase__ : List[str] = self.pipeline_class(**a ) lowerCAmelCase__ : Dict = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : str = pipe(**a ) lowerCAmelCase__ : Optional[Any] = output.images lowerCAmelCase__ : str = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : List[str] = np.array( [ 0.9_9_9_7, 0.0_0_0_2, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_6_9, 0.0_0_2_3, 0.9_9_9_7, 0.9_9_6_9, 0.9_9_7_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 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = 'cpu' lowerCAmelCase__ : Dict = self.get_dummy_components() lowerCAmelCase__ : Optional[int] = self.pipeline_class(**a ) lowerCAmelCase__ : int = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = pipe(**a ) lowerCAmelCase__ : Union[str, Any] = output.images lowerCAmelCase__ : int = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : int = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Tuple = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : str = np.array([0.9_9_9_7, 0.0_0_0_3, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_7_0, 0.0_0_2_4, 0.9_9_9_7, 0.9_9_7_1, 0.9_9_7_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Tuple = 'cpu' lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : Tuple = self.pipeline_class(**a ) lowerCAmelCase__ : Union[str, Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Tuple = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = [ pipeline_inputs['image'], pipeline_inputs['image'], ] lowerCAmelCase__ : Optional[int] = pipe(**a ) lowerCAmelCase__ : Tuple = output.images lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Union[str, Any] = [ tuple_pipeline_inputs['image'], tuple_pipeline_inputs['image'], ] lowerCAmelCase__ : str = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Any = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (2, 64, 64, 3) lowerCAmelCase__ : Union[str, Any] = np.array( [ 0.9_9_9_7, 0.9_9_8_9, 0.0_0_0_8, 0.0_0_2_1, 0.9_9_6_0, 0.0_0_1_8, 0.0_0_1_4, 0.0_0_0_2, 0.9_9_3_3, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch.device('cpu' ) class A__ : lowercase = 1 lowerCAmelCase__ : Optional[Any] = self.get_dummy_components() lowerCAmelCase__ : Dict = self.pipeline_class(**a ) lowerCAmelCase__ : Optional[Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.Generator(device=a ).manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe.decoder.dtype lowerCAmelCase__ : Union[str, Any] = 1 lowerCAmelCase__ : str = ( batch_size, pipe.decoder.config.in_channels, pipe.decoder.config.sample_size, pipe.decoder.config.sample_size, ) lowerCAmelCase__ : List[Any] = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[str] = ( batch_size, pipe.super_res_first.config.in_channels // 2, pipe.super_res_first.config.sample_size, pipe.super_res_first.config.sample_size, ) lowerCAmelCase__ : Any = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , decoder_latents=a , super_res_latents=a ).images lowerCAmelCase__ : Optional[Any] = self.get_dummy_inputs(a , pil_image=a ) # Don't pass image, instead pass embedding lowerCAmelCase__ : Union[str, Any] = pipeline_inputs.pop('image' ) lowerCAmelCase__ : Union[str, Any] = pipe.image_encoder(a ).image_embeds lowerCAmelCase__ : List[Any] = pipe( **a , decoder_latents=a , super_res_latents=a , image_embeddings=a , ).images # make sure passing text embeddings manually is identical assert np.abs(img_out_a - img_out_a ).max() < 1E-4 @skip_mps def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device == 'cpu' # Check is relaxed because there is not a torch 2.0 sliced attention added kv processor lowerCAmelCase__ : int = 1E-2 self._test_attention_slicing_forward_pass( test_max_difference=a , expected_max_diff=a ) @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = torch_device == 'cpu' lowerCAmelCase__ : Any = True lowerCAmelCase__ : Optional[Any] = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] self._test_inference_batch_single_identical( test_max_difference=a , relax_max_difference=a , additional_params_copy_to_batched_inputs=a , ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] if torch_device == "mps": # TODO: MPS errors with larger batch sizes lowerCAmelCase__ : List[str] = [2, 3] self._test_inference_batch_consistent( batch_sizes=a , additional_params_copy_to_batched_inputs=a , ) else: self._test_inference_batch_consistent( additional_params_copy_to_batched_inputs=a ) @skip_mps def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent() @skip_mps def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_optional_components() @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Tuple ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/unclip/cat.png' ) lowerCAmelCase__ : List[Any] = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/unclip/karlo_v1_alpha_cat_variation_fp16.npy' ) lowerCAmelCase__ : Tuple = UnCLIPImageVariationPipeline.from_pretrained( 'kakaobrain/karlo-v1-alpha-image-variations' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Union[str, Any] = pipeline.to(a ) pipeline.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = torch.Generator(device='cpu' ).manual_seed(0 ) lowerCAmelCase__ : List[str] = pipeline( a , generator=a , output_type='np' , ) lowerCAmelCase__ : Union[str, Any] = output.images[0] assert image.shape == (256, 256, 3) assert_mean_pixel_difference(a , a , 15 )
307
0
import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase__ = logging.get_logger(__name__) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: lowerCAmelCase__ : List[str] = torch.load(a_ , map_location='cpu' ) if "model" in sd.keys(): lowerCAmelCase__ : Union[str, Any] = torch.load(a_ , map_location='cpu' )['model'] # pop unnecessary weights lowerCAmelCase__ : int = [ 'decoder.version', 'decoder.output_projection.weight', ] for key in keys_to_delete: if key in sd: sd.pop(a_ ) lowerCAmelCase__ : Optional[int] = { '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: lowerCAmelCase__ : Tuple = sd.pop(a_ ) lowerCAmelCase__ : Optional[Any] = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: lowerCAmelCase__ : Union[str, Any] = sd[key] # We split QKV in separate Q,K,V lowerCAmelCase__ : Dict = key.replace('.qkv_proj.' , '.q_proj.' ) lowerCAmelCase__ : Union[str, Any] = key.replace('.qkv_proj.' , '.k_proj.' ) lowerCAmelCase__ : Dict = key.replace('.qkv_proj.' , '.v_proj.' ) lowerCAmelCase__ : Optional[int] = 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 lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = torch.split(a_ , depth // 3 , dim=0 ) lowerCAmelCase__ : Optional[Any] = q lowerCAmelCase__ : List[str] = k lowerCAmelCase__ : Optional[int] = v del sd[key] return sd @torch.no_grad() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> int: lowerCAmelCase__ : Tuple = load_checkpoint(a_ ) if config is not None: lowerCAmelCase__ : str = OPTConfig.from_pretrained(a_ ) else: lowerCAmelCase__ : int = OPTConfig() lowerCAmelCase__ : Any = OPTModel(a_ ).half().eval() model.load_state_dict(a_ ) # Check results Path(a_ ).mkdir(exist_ok=a_ ) model.save_pretrained(a_ ) if __name__ == "__main__": lowerCamelCase__ = 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.""") lowerCamelCase__ = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
364
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: stooge(SCREAMING_SNAKE_CASE_ , 0 , len(SCREAMING_SNAKE_CASE_ ) - 1 ) return arr def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowerCAmelCase__ , lowerCAmelCase__ : Tuple = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowerCAmelCase__ : Union[str, Any] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) # Recursively sort last 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , i + t , (SCREAMING_SNAKE_CASE_) ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) if __name__ == "__main__": lowerCamelCase__ = input("""Enter numbers separated by a comma:\n""").strip() lowerCamelCase__ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
307
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """studio-ousia/luke-base""": """https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json""", """studio-ousia/luke-large""": """https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json""", } class A__ ( __lowercase ): lowercase = "luke" def __init__( self : Union[str, Any] , a : str=50_267 , a : Optional[int]=500_000 , a : Optional[int]=768 , a : Optional[Any]=256 , a : Optional[int]=12 , a : str=12 , a : int=3_072 , a : Tuple="gelu" , a : Any=0.1 , a : List[str]=0.1 , a : Union[str, Any]=512 , a : Union[str, Any]=2 , a : Tuple=0.0_2 , a : Any=1E-12 , a : List[Any]=True , a : Optional[int]=None , a : Tuple=1 , a : Optional[int]=0 , a : Optional[int]=2 , **a : int , ): super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) lowerCAmelCase__ : List[Any] = vocab_size lowerCAmelCase__ : List[Any] = entity_vocab_size lowerCAmelCase__ : Optional[int] = hidden_size lowerCAmelCase__ : Optional[Any] = entity_emb_size lowerCAmelCase__ : List[Any] = num_hidden_layers lowerCAmelCase__ : Optional[int] = num_attention_heads lowerCAmelCase__ : Any = hidden_act lowerCAmelCase__ : Any = intermediate_size lowerCAmelCase__ : Dict = hidden_dropout_prob lowerCAmelCase__ : Optional[int] = attention_probs_dropout_prob lowerCAmelCase__ : Tuple = max_position_embeddings lowerCAmelCase__ : Optional[Any] = type_vocab_size lowerCAmelCase__ : int = initializer_range lowerCAmelCase__ : List[str] = layer_norm_eps lowerCAmelCase__ : Dict = use_entity_aware_attention lowerCAmelCase__ : Optional[int] = classifier_dropout
365
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) lowerCamelCase__ = { """configuration_speech_to_text""": ["""SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Speech2TextConfig"""], """processing_speech_to_text""": ["""Speech2TextProcessor"""], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextTokenizer"""] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextFeatureExtractor"""] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFSpeech2TextForConditionalGeneration""", """TFSpeech2TextModel""", """TFSpeech2TextPreTrainedModel""", ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """Speech2TextForConditionalGeneration""", """Speech2TextModel""", """Speech2TextPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
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, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowerCamelCase__ = logging.get_logger(__name__) class A__ ( __SCREAMING_SNAKE_CASE ): lowercase = ["pixel_values"] def __init__( self : Tuple , a : Union[str, Any] = True , a : Union[str, Any] = None , a : Optional[Any] = 0.9 , a : Any = PILImageResampling.BICUBIC , a : Union[str, Any] = True , a : Optional[Any] = None , a : int = 1 / 255 , a : Optional[int] = True , a : int = True , a : Optional[int] = None , a : Optional[int] = None , **a : Optional[int] , ): '''simple docstring''' super().__init__(**__UpperCAmelCase ) lowerCAmelCase__ : List[str] = size if size is not None else {"""shortest_edge""": 224} lowerCAmelCase__ : Any = get_size_dict(__UpperCAmelCase , default_to_square=__UpperCAmelCase ) lowerCAmelCase__ : Optional[int] = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} lowerCAmelCase__ : Union[str, Any] = get_size_dict(__UpperCAmelCase , param_name='crop_size' ) lowerCAmelCase__ : Dict = do_resize lowerCAmelCase__ : int = size lowerCAmelCase__ : Optional[int] = crop_pct lowerCAmelCase__ : int = resample lowerCAmelCase__ : int = do_center_crop lowerCAmelCase__ : int = crop_size lowerCAmelCase__ : List[Any] = do_rescale lowerCAmelCase__ : int = rescale_factor lowerCAmelCase__ : Dict = do_normalize lowerCAmelCase__ : Any = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN lowerCAmelCase__ : Optional[Any] = image_std if image_std is not None else IMAGENET_DEFAULT_STD def _lowerCamelCase ( self : int , a : str , a : Dict , a : Optional[Any] = None , a : Union[str, Any] = PILImageResampling.BICUBIC , a : Optional[Any] = None , **a : Tuple , ): '''simple docstring''' lowerCAmelCase__ : List[str] = get_size_dict(__UpperCAmelCase , default_to_square=__UpperCAmelCase ) if "shortest_edge" not in size and ("height" not in size or "width" not in size): raise ValueError(f'''size must contain \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) if crop_pct is not None: if "shortest_edge" in size: lowerCAmelCase__ : Tuple = int(size['shortest_edge'] / crop_pct ) elif "height" in size and "width" in size: if size["height"] == size["width"]: lowerCAmelCase__ : List[Any] = int(size['height'] / crop_pct ) else: lowerCAmelCase__ : str = (int(size['height'] / crop_pct ), int(size['width'] / crop_pct )) else: raise ValueError('Invalid size for resize: {}'.format(__UpperCAmelCase ) ) lowerCAmelCase__ : Union[str, Any] = get_resize_output_image_size(__UpperCAmelCase , size=__UpperCAmelCase , default_to_square=__UpperCAmelCase ) else: if "shortest_edge" in size: lowerCAmelCase__ : Tuple = get_resize_output_image_size(__UpperCAmelCase , size=size['shortest_edge'] , default_to_square=__UpperCAmelCase ) elif "height" in size and "width" in size: lowerCAmelCase__ : int = (size["""height"""], size["""width"""]) else: raise ValueError('Invalid size for resize: {}'.format(__UpperCAmelCase ) ) return resize(__UpperCAmelCase , size=__UpperCAmelCase , resample=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def _lowerCamelCase ( self : Dict , a : Optional[int] , a : Optional[int] , a : int = None , **a : Any , ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = get_size_dict(__UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''size must contain \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(__UpperCAmelCase , size=(size['height'], size['width']) , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def _lowerCamelCase ( self : List[Any] , a : str , a : Optional[Any] , a : Optional[Any] = None , **a : Union[str, Any] , ): '''simple docstring''' return rescale(__UpperCAmelCase , scale=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def _lowerCamelCase ( self : int , a : Optional[int] , a : Any , a : Tuple , a : Any = None , **a : Optional[Any] , ): '''simple docstring''' return normalize(__UpperCAmelCase , mean=__UpperCAmelCase , std=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def _lowerCamelCase ( self : Any , a : List[Any] , a : Optional[int] = None , a : List[str] = None , a : int = None , a : Optional[Any] = None , a : Tuple = None , a : Union[str, Any] = None , a : Union[str, Any] = None , a : List[Any] = None , a : Any = None , a : Optional[Any] = None , a : Any = None , a : Tuple = None , a : List[str] = ChannelDimension.FIRST , **a : Union[str, Any] , ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = do_resize if do_resize is not None else self.do_resize lowerCAmelCase__ : int = crop_pct if crop_pct is not None else self.crop_pct lowerCAmelCase__ : Tuple = resample if resample is not None else self.resample lowerCAmelCase__ : int = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCAmelCase__ : Optional[Any] = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase__ : Dict = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase__ : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase__ : List[Any] = image_mean if image_mean is not None else self.image_mean lowerCAmelCase__ : Dict = image_std if image_std is not None else self.image_std lowerCAmelCase__ : Any = size if size is not None else self.size lowerCAmelCase__ : Any = get_size_dict(__UpperCAmelCase , default_to_square=__UpperCAmelCase ) lowerCAmelCase__ : int = crop_size if crop_size is not None else self.crop_size lowerCAmelCase__ : Optional[Any] = get_size_dict(__UpperCAmelCase , param_name='crop_size' ) lowerCAmelCase__ : List[Any] = make_list_of_images(__UpperCAmelCase ) if not valid_images(__UpperCAmelCase ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.' ) if do_center_crop and crop_pct is None: raise ValueError('Crop_pct must be specified if do_center_crop is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) # All transformations expect numpy arrays. lowerCAmelCase__ : Union[str, Any] = [to_numpy_array(__UpperCAmelCase ) for image in images] if do_resize: lowerCAmelCase__ : int = [self.resize(image=__UpperCAmelCase , size=__UpperCAmelCase , crop_pct=__UpperCAmelCase , resample=__UpperCAmelCase ) for image in images] if do_center_crop: lowerCAmelCase__ : Union[str, Any] = [self.center_crop(image=__UpperCAmelCase , size=__UpperCAmelCase ) for image in images] if do_rescale: lowerCAmelCase__ : List[str] = [self.rescale(image=__UpperCAmelCase , scale=__UpperCAmelCase ) for image in images] if do_normalize: lowerCAmelCase__ : Optional[int] = [self.normalize(image=__UpperCAmelCase , mean=__UpperCAmelCase , std=__UpperCAmelCase ) for image in images] lowerCAmelCase__ : Union[str, Any] = [to_channel_dimension_format(__UpperCAmelCase , __UpperCAmelCase ) for image in images] lowerCAmelCase__ : Dict = {"""pixel_values""": images} return BatchFeature(data=__UpperCAmelCase , tensor_type=__UpperCAmelCase )
366
import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = { 'task_specific_params': { 'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4}, 'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4}, 'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6}, } } lowerCAmelCase__ : int = { 'task_specific_params.summarization.length_penalty': 1.0, 'task_specific_params.summarization.max_length': 128, 'task_specific_params.summarization.min_length': 12, 'task_specific_params.summarization.num_beams': 4, 'task_specific_params.summarization_cnn.length_penalty': 2.0, 'task_specific_params.summarization_cnn.max_length': 142, 'task_specific_params.summarization_cnn.min_length': 56, 'task_specific_params.summarization_cnn.num_beams': 4, 'task_specific_params.summarization_xsum.length_penalty': 1.0, 'task_specific_params.summarization_xsum.max_length': 62, 'task_specific_params.summarization_xsum.min_length': 11, 'task_specific_params.summarization_xsum.num_beams': 6, } self.assertEqual(flatten_dict(a ) , a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(a ) , x.transpose() ) ) lowerCAmelCase__ : List[str] = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : int = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Dict = tf.constant(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : int = jnp.array(a ) self.assertTrue(np.allclose(transpose(a ) , np.asarray(transpose(a ) ) ) ) lowerCAmelCase__ : Any = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = jnp.array(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , np.asarray(transpose(a , axes=(1, 2, 0) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.reshape(a , (4, 3) ) ) ) lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.reshape(a , (12, 5) ) ) ) @require_torch def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : Dict = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[str] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.asarray(reshape(a , (4, 3) ) ) ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.asarray(reshape(a , (12, 5) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(a ) , np.squeeze(a ) ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.squeeze(a , axis=2 ) ) ) @require_torch def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : Optional[Any] = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : str = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a ) , np.asarray(squeeze(a ) ) ) ) lowerCAmelCase__ : str = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Optional[Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.asarray(squeeze(a , axis=2 ) ) ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.expand_dims(a , axis=1 ) ) ) @require_torch def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : str = np.random.randn(3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = np.random.randn(3 , 4 ) lowerCAmelCase__ : Tuple = jnp.array(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.asarray(expand_dims(a , axis=1 ) ) ) )
307
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """RWKV/rwkv-4-169m-pile""": """https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json""", """RWKV/rwkv-4-430m-pile""": """https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json""", """RWKV/rwkv-4-1b5-pile""": """https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json""", """RWKV/rwkv-4-3b-pile""": """https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json""", """RWKV/rwkv-4-7b-pile""": """https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json""", """RWKV/rwkv-4-14b-pile""": """https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json""", """RWKV/rwkv-raven-1b5""": """https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json""", """RWKV/rwkv-raven-3b""": """https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json""", """RWKV/rwkv-raven-7b""": """https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json""", """RWKV/rwkv-raven-14b""": """https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json""", } class A__ ( __magic_name__ ): lowercase = '''rwkv''' lowercase = {'''max_position_embeddings''': '''context_length'''} def __init__( self : Union[str, Any] , a : List[str]=50_277 , a : Optional[int]=1_024 , a : Any=4_096 , a : Dict=32 , a : Union[str, Any]=None , a : str=None , a : Dict=1E-5 , a : List[Any]=0 , a : List[str]=0 , a : int=6 , a : Any=False , a : List[str]=True , **a : int , ): '''simple docstring''' lowerCAmelCase__ : Dict = vocab_size lowerCAmelCase__ : Optional[int] = context_length lowerCAmelCase__ : str = hidden_size lowerCAmelCase__ : Dict = num_hidden_layers lowerCAmelCase__ : Union[str, Any] = attention_hidden_size if attention_hidden_size is not None else hidden_size lowerCAmelCase__ : str = intermediate_size if intermediate_size is not None else 4 * hidden_size lowerCAmelCase__ : Union[str, Any] = layer_norm_epsilon lowerCAmelCase__ : str = rescale_every lowerCAmelCase__ : Optional[Any] = use_cache lowerCAmelCase__ : List[str] = bos_token_id lowerCAmelCase__ : Union[str, Any] = eos_token_id super().__init__( tie_word_embeddings=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__ )
367
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position lowerCamelCase__ = """2.13.1""" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("""3.7"""): raise ImportWarning( """To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition.""" ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( """To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n""" """If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.""" ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip lowerCamelCase__ = concatenate_datasets lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadManager lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: while b: lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = b, a % b return a def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: return a if b == 0 else euclidean_gcd_recursive(_UpperCamelCase , a % b ) def lowerCAmelCase__ ( ) -> List[str]: print(F'''euclidean_gcd(3, 5) = {euclidean_gcd(3 , 5 )}''' ) print(F'''euclidean_gcd(5, 3) = {euclidean_gcd(5 , 3 )}''' ) print(F'''euclidean_gcd(1, 3) = {euclidean_gcd(1 , 3 )}''' ) print(F'''euclidean_gcd(3, 6) = {euclidean_gcd(3 , 6 )}''' ) print(F'''euclidean_gcd(6, 3) = {euclidean_gcd(6 , 3 )}''' ) print(F'''euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3 , 5 )}''' ) print(F'''euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5 , 3 )}''' ) print(F'''euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1 , 3 )}''' ) print(F'''euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3 , 6 )}''' ) print(F'''euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6 , 3 )}''' ) if __name__ == "__main__": main()
368
import gc import unittest import numpy as np import torch from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = DanceDiffusionPipeline lowercase = UNCONDITIONAL_AUDIO_GENERATION_PARAMS lowercase = PipelineTesterMixin.required_optional_params - { 'callback', 'latents', 'callback_steps', 'output_type', 'num_images_per_prompt', } lowercase = UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS lowercase = False lowercase = False def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = UNetaDModel( block_out_channels=(32, 32, 64) , extra_in_channels=16 , sample_size=512 , sample_rate=16_000 , in_channels=2 , out_channels=2 , flip_sin_to_cos=a , use_timestep_embedding=a , time_embedding_type='fourier' , mid_block_type='UNetMidBlock1D' , down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D') , up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip') , ) lowerCAmelCase__ : Tuple = IPNDMScheduler() lowerCAmelCase__ : str = { 'unet': unet, 'scheduler': scheduler, } return components def _lowerCamelCase ( self : int , a : Dict , a : List[str]=0 ): '''simple docstring''' if str(a ).startswith('mps' ): lowerCAmelCase__ : Union[str, Any] = torch.manual_seed(a ) else: lowerCAmelCase__ : Optional[Any] = torch.Generator(device=a ).manual_seed(a ) lowerCAmelCase__ : Optional[Any] = { 'batch_size': 1, 'generator': generator, 'num_inference_steps': 4, } return inputs def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'cpu' # ensure determinism for the device-dependent torch.Generator lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : List[str] = DanceDiffusionPipeline(**a ) lowerCAmelCase__ : Any = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a ) lowerCAmelCase__ : List[Any] = pipe(**a ) lowerCAmelCase__ : List[str] = output.audios lowerCAmelCase__ : Optional[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, components["unet"].sample_size) lowerCAmelCase__ : List[Any] = np.array([-0.7_2_6_5, 1.0_0_0_0, -0.8_3_8_8, 0.1_1_7_5, 0.9_4_9_8, -1.0_0_0_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) @skip_mps def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return super().test_save_load_optional_components() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_attention_slicing_forward_pass() def _lowerCamelCase ( self : List[str] ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Dict ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device lowerCAmelCase__ : List[str] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' ) lowerCAmelCase__ : List[str] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : int = output.audios lowerCAmelCase__ : List[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : Dict = np.array([-0.0_1_9_2, -0.0_2_3_1, -0.0_3_1_8, -0.0_0_5_9, 0.0_0_0_2, -0.0_0_2_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = torch_device lowerCAmelCase__ : List[Any] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Optional[int] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : str = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : str = output.audios lowerCAmelCase__ : Tuple = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : int = np.array([-0.0_3_6_7, -0.0_4_8_8, -0.0_7_7_1, -0.0_5_2_5, -0.0_4_4_4, -0.0_3_4_1] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2
307
0
import copy import random from transformers import CLIPTokenizer class A__ ( a__ ): def __init__( self : List[Any] , *a : str , **a : List[Any] ): '''simple docstring''' super().__init__(*_lowerCamelCase , **_lowerCamelCase ) lowerCAmelCase__ : Any = {} def _lowerCamelCase ( self : Any , a : Optional[int] , *a : int , **a : str ): '''simple docstring''' lowerCAmelCase__ : int = super().add_tokens(_lowerCamelCase , *_lowerCamelCase , **_lowerCamelCase ) if num_added_tokens == 0: raise ValueError( f'''The tokenizer already contains the token {placeholder_token}. Please pass a different''' ' `placeholder_token` that is not already in the tokenizer.' ) def _lowerCamelCase ( self : str , a : int , *a : Union[str, Any] , a : str=1 , **a : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = [] if num_vec_per_token == 1: self.try_adding_tokens(_lowerCamelCase , *_lowerCamelCase , **_lowerCamelCase ) output.append(_lowerCamelCase ) else: lowerCAmelCase__ : Union[str, Any] = [] for i in range(_lowerCamelCase ): lowerCAmelCase__ : Optional[int] = placeholder_token + f'''_{i}''' self.try_adding_tokens(_lowerCamelCase , *_lowerCamelCase , **_lowerCamelCase ) output.append(_lowerCamelCase ) # handle cases where there is a new placeholder token that contains the current placeholder token but is larger for token in self.token_map: if token in placeholder_token: raise ValueError( f'''The tokenizer already has placeholder token {token} that can get confused with''' f''' {placeholder_token}keep placeholder tokens independent''' ) lowerCAmelCase__ : int = output def _lowerCamelCase ( self : List[str] , a : Tuple , a : int=False , a : Optional[int]=1.0 ): '''simple docstring''' if isinstance(_lowerCamelCase , _lowerCamelCase ): lowerCAmelCase__ : List[Any] = [] for i in range(len(_lowerCamelCase ) ): output.append(self.replace_placeholder_tokens_in_text(text[i] , vector_shuffle=_lowerCamelCase ) ) return output for placeholder_token in self.token_map: if placeholder_token in text: lowerCAmelCase__ : str = self.token_map[placeholder_token] lowerCAmelCase__ : int = tokens[: 1 + int(len(_lowerCamelCase ) * prop_tokens_to_load )] if vector_shuffle: lowerCAmelCase__ : Tuple = copy.copy(_lowerCamelCase ) random.shuffle(_lowerCamelCase ) lowerCAmelCase__ : List[Any] = text.replace(_lowerCamelCase , ' '.join(_lowerCamelCase ) ) return text def __call__( self : Optional[int] , a : Tuple , *a : int , a : Optional[int]=False , a : int=1.0 , **a : List[Any] ): '''simple docstring''' return super().__call__( self.replace_placeholder_tokens_in_text( _lowerCamelCase , vector_shuffle=_lowerCamelCase , prop_tokens_to_load=_lowerCamelCase ) , *_lowerCamelCase , **_lowerCamelCase , ) def _lowerCamelCase ( self : List[Any] , a : List[Any] , *a : List[Any] , a : Optional[Any]=False , a : Any=1.0 , **a : List[Any] ): '''simple docstring''' return super().encode( self.replace_placeholder_tokens_in_text( _lowerCamelCase , vector_shuffle=_lowerCamelCase , prop_tokens_to_load=_lowerCamelCase ) , *_lowerCamelCase , **_lowerCamelCase , )
369
from ..utils import DummyObject, requires_backends class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Union[str, Any] , *a : Optional[int] , **a : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : List[Any] , **a : int ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Any , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Optional[Any] , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Optional[int] , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : List[Any] , *a : List[str] , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Union[str, Any] , **a : Optional[int] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : List[Any] , *a : Dict , **a : List[str] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Dict , **a : List[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Dict , *a : str , **a : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Union[str, Any] , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : Union[str, Any] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Tuple , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] )
307
0
from typing import Tuple, Union from ...modeling_outputs import BackboneOutput from ...modeling_utils import PreTrainedModel from ...utils import is_timm_available, is_torch_available, requires_backends from ...utils.backbone_utils import BackboneMixin from .configuration_timm_backbone import TimmBackboneConfig if is_timm_available(): import timm if is_torch_available(): from torch import Tensor class A__ ( __UpperCamelCase , __UpperCamelCase ): lowercase = "pixel_values" lowercase = False lowercase = TimmBackboneConfig def __init__( self : Tuple , a : Optional[int] , **a : Any ): '''simple docstring''' requires_backends(self , 'timm' ) super().__init__(_lowerCAmelCase ) lowerCAmelCase__ : str = config if config.backbone is None: raise ValueError('backbone is not set in the config. Please set it to a timm model name.' ) if config.backbone not in timm.list_models(): raise ValueError(f'''backbone {config.backbone} is not supported by timm.''' ) if hasattr(_lowerCAmelCase , 'out_features' ) and config.out_features is not None: raise ValueError('out_features is not supported by TimmBackbone. Please use out_indices instead.' ) lowerCAmelCase__ : List[Any] = getattr(_lowerCAmelCase , 'use_pretrained_backbone' , _lowerCAmelCase ) if pretrained is None: raise ValueError('use_pretrained_backbone is not set in the config. Please set it to True or False.' ) # We just take the final layer by default. This matches the default for the transformers models. lowerCAmelCase__ : Tuple = config.out_indices if getattr(_lowerCAmelCase , 'out_indices' , _lowerCAmelCase ) is not None else (-1,) lowerCAmelCase__ : Any = timm.create_model( config.backbone , pretrained=_lowerCAmelCase , features_only=config.features_only , in_chans=config.num_channels , out_indices=_lowerCAmelCase , **_lowerCAmelCase , ) # These are used to control the output of the model when called. If output_hidden_states is True, then # return_layers is modified to include all layers. lowerCAmelCase__ : Optional[int] = self._backbone.return_layers lowerCAmelCase__ : Dict = {layer["""module"""]: str(_lowerCAmelCase ) for i, layer in enumerate(self._backbone.feature_info.info )} super()._init_backbone(_lowerCAmelCase ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , a : List[Any] , *a : str , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['vision', 'timm'] ) from ...models.timm_backbone import TimmBackboneConfig lowerCAmelCase__ : List[str] = kwargs.pop('config' , TimmBackboneConfig() ) lowerCAmelCase__ : Optional[Any] = kwargs.pop('use_timm_backbone' , _lowerCAmelCase ) if not use_timm: raise ValueError('use_timm_backbone must be True for timm backbones' ) lowerCAmelCase__ : int = kwargs.pop('num_channels' , config.num_channels ) lowerCAmelCase__ : List[Any] = kwargs.pop('features_only' , config.features_only ) lowerCAmelCase__ : List[str] = kwargs.pop('use_pretrained_backbone' , config.use_pretrained_backbone ) lowerCAmelCase__ : Union[str, Any] = kwargs.pop('out_indices' , config.out_indices ) lowerCAmelCase__ : List[Any] = TimmBackboneConfig( backbone=_lowerCAmelCase , num_channels=_lowerCAmelCase , features_only=_lowerCAmelCase , use_pretrained_backbone=_lowerCAmelCase , out_indices=_lowerCAmelCase , ) return super()._from_config(_lowerCAmelCase , **_lowerCAmelCase ) def _lowerCamelCase ( self : Optional[int] , a : Optional[int] ): '''simple docstring''' pass def _lowerCamelCase ( self : int , a : List[str] , a : Optional[int]=None , a : Union[str, Any]=None , a : List[str]=None , **a : int ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = return_dict if return_dict is not None else self.config.use_return_dict lowerCAmelCase__ : int = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowerCAmelCase__ : Union[str, Any] = output_attentions if output_attentions is not None else self.config.output_attentions if output_attentions: raise ValueError('Cannot output attentions for timm backbones at the moment' ) if output_hidden_states: # We modify the return layers to include all the stages of the backbone lowerCAmelCase__ : Tuple = self._all_layers lowerCAmelCase__ : Union[str, Any] = self._backbone(_lowerCAmelCase , **_lowerCAmelCase ) lowerCAmelCase__ : Any = self._return_layers lowerCAmelCase__ : Any = tuple(hidden_states[i] for i in self.out_indices ) else: lowerCAmelCase__ : Union[str, Any] = self._backbone(_lowerCAmelCase , **_lowerCAmelCase ) lowerCAmelCase__ : Any = None lowerCAmelCase__ : str = tuple(_lowerCAmelCase ) lowerCAmelCase__ : Tuple = tuple(_lowerCAmelCase ) if hidden_states is not None else None if not return_dict: lowerCAmelCase__ : Any = (feature_maps,) if output_hidden_states: lowerCAmelCase__ : int = output + (hidden_states,) return output return BackboneOutput(feature_maps=_lowerCAmelCase , hidden_states=_lowerCAmelCase , attentions=_lowerCAmelCase )
370
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class A__ : def __init__( self : List[str] , a : Any , a : Dict=13 , a : Optional[Any]=7 , a : Tuple=True , a : Tuple=True , a : Dict=False , a : Optional[Any]=True , a : Dict=99 , a : Tuple=32 , a : Optional[Any]=5 , a : str=4 , a : Union[str, Any]=37 , a : Any="gelu" , a : Dict=0.1 , a : Any=0.1 , a : Optional[int]=512 , a : Union[str, Any]=16 , a : Optional[int]=2 , a : Optional[Any]=0.0_2 , a : List[Any]=3 , a : Any=4 , a : Optional[int]=None , ): '''simple docstring''' lowerCAmelCase__ : List[str] = parent lowerCAmelCase__ : str = batch_size lowerCAmelCase__ : Optional[int] = seq_length lowerCAmelCase__ : Optional[Any] = is_training lowerCAmelCase__ : Tuple = use_input_mask lowerCAmelCase__ : List[Any] = use_token_type_ids lowerCAmelCase__ : str = use_labels lowerCAmelCase__ : Dict = vocab_size lowerCAmelCase__ : Union[str, Any] = hidden_size lowerCAmelCase__ : Optional[int] = num_hidden_layers lowerCAmelCase__ : List[Any] = num_attention_heads lowerCAmelCase__ : int = intermediate_size lowerCAmelCase__ : Union[str, Any] = hidden_act lowerCAmelCase__ : Union[str, Any] = hidden_dropout_prob lowerCAmelCase__ : Any = attention_probs_dropout_prob lowerCAmelCase__ : Dict = max_position_embeddings lowerCAmelCase__ : int = type_vocab_size lowerCAmelCase__ : int = type_sequence_label_size lowerCAmelCase__ : Dict = initializer_range lowerCAmelCase__ : List[str] = num_labels lowerCAmelCase__ : Any = num_choices lowerCAmelCase__ : List[Any] = scope def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase__ : Tuple = None if self.use_input_mask: lowerCAmelCase__ : str = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase__ : List[str] = None if self.use_token_type_ids: lowerCAmelCase__ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCAmelCase__ : Optional[Any] = None lowerCAmelCase__ : Dict = None lowerCAmelCase__ : str = None if self.use_labels: lowerCAmelCase__ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCAmelCase__ : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) lowerCAmelCase__ : List[str] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return LlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=a , initializer_range=self.initializer_range , ) def _lowerCamelCase ( self : Tuple , a : Dict , a : List[str] , a : str , a : Union[str, Any] , a : Optional[Any] , a : Dict , a : str ): '''simple docstring''' lowerCAmelCase__ : str = LlamaModel(config=a ) model.to(a ) model.eval() lowerCAmelCase__ : Dict = model(a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCamelCase ( self : int , a : Any , a : Union[str, Any] , a : Dict , a : Dict , a : List[Any] , a : Optional[Any] , a : int , a : Dict , a : Tuple , ): '''simple docstring''' lowerCAmelCase__ : int = True lowerCAmelCase__ : Dict = LlamaModel(a ) model.to(a ) model.eval() lowerCAmelCase__ : List[Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , ) lowerCAmelCase__ : Optional[int] = model( a , attention_mask=a , encoder_hidden_states=a , ) lowerCAmelCase__ : Union[str, Any] = model(a , attention_mask=a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCamelCase ( self : Union[str, Any] , a : int , a : List[Any] , a : int , a : Tuple , a : List[Any] , a : Union[str, Any] , a : Any , a : List[str] , a : List[str] , ): '''simple docstring''' lowerCAmelCase__ : List[Any] = LlamaForCausalLM(config=a ) model.to(a ) model.eval() lowerCAmelCase__ : Tuple = model(a , attention_mask=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowerCamelCase ( self : str , a : Any , a : Tuple , a : str , a : Union[str, Any] , a : Optional[Any] , a : List[Any] , a : Optional[Any] , a : Optional[Any] , a : List[str] , ): '''simple docstring''' lowerCAmelCase__ : str = True lowerCAmelCase__ : Optional[int] = True lowerCAmelCase__ : List[Any] = LlamaForCausalLM(config=a ) model.to(a ) model.eval() # first forward pass lowerCAmelCase__ : List[str] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , use_cache=a , ) lowerCAmelCase__ : List[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids lowerCAmelCase__ : Union[str, Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowerCAmelCase__ : Tuple = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and lowerCAmelCase__ : int = torch.cat([input_ids, next_tokens] , dim=-1 ) lowerCAmelCase__ : Any = torch.cat([input_mask, next_mask] , dim=-1 ) lowerCAmelCase__ : Union[str, Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , output_hidden_states=a , )['hidden_states'][0] lowerCAmelCase__ : Union[str, Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , past_key_values=a , output_hidden_states=a , )['hidden_states'][0] # select random slice lowerCAmelCase__ : Any = ids_tensor((1,) , output_from_past.shape[-1] ).item() lowerCAmelCase__ : Optional[int] = output_from_no_past[:, -3:, random_slice_idx].detach() lowerCAmelCase__ : Any = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(a , a , atol=1E-3 ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.prepare_config_and_inputs() ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : Any = config_and_inputs lowerCAmelCase__ : List[str] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class A__ ( __magic_name__ , __magic_name__ , __magic_name__ , unittest.TestCase ): lowercase = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () lowercase = (LlamaForCausalLM,) if is_torch_available() else () lowercase = ( { 'feature-extraction': LlamaModel, 'text-classification': LlamaForSequenceClassification, 'text-generation': LlamaForCausalLM, 'zero-shot': LlamaForSequenceClassification, } if is_torch_available() else {} ) lowercase = False lowercase = False def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Tuple = LlamaModelTester(self ) lowerCAmelCase__ : str = ConfigTester(self , config_class=a , hidden_size=37 ) def _lowerCamelCase ( self : str ): '''simple docstring''' self.config_tester.run_common_tests() def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCAmelCase__ : int = type self.model_tester.create_and_check_model(*a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : int = 3 lowerCAmelCase__ : Dict = input_dict['input_ids'] lowerCAmelCase__ : Optional[Any] = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Tuple = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : Tuple = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : str = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : List[Any] = 3 lowerCAmelCase__ : List[str] = 'single_label_classification' lowerCAmelCase__ : List[Any] = input_dict['input_ids'] lowerCAmelCase__ : List[Any] = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Optional[int] = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : int = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : Optional[int] = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Optional[Any] = 3 lowerCAmelCase__ : Optional[Any] = 'multi_label_classification' lowerCAmelCase__ : List[str] = input_dict['input_ids'] lowerCAmelCase__ : Tuple = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Any = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) lowerCAmelCase__ : Dict = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : Dict = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' pass @parameterized.expand([('linear',), ('dynamic',)] ) def _lowerCamelCase ( self : Optional[int] , a : Dict ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Tuple = ids_tensor([1, 10] , config.vocab_size ) lowerCAmelCase__ : List[str] = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : List[Any] = LlamaModel(a ) original_model.to(a ) original_model.eval() lowerCAmelCase__ : List[Any] = original_model(a ).last_hidden_state lowerCAmelCase__ : str = original_model(a ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : Any = {'type': scaling_type, 'factor': 1_0.0} lowerCAmelCase__ : Union[str, Any] = LlamaModel(a ) scaled_model.to(a ) scaled_model.eval() lowerCAmelCase__ : Union[str, Any] = scaled_model(a ).last_hidden_state lowerCAmelCase__ : Optional[int] = scaled_model(a ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(a , a , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(a , a , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(a , a , atol=1E-5 ) ) @require_torch class A__ ( unittest.TestCase ): @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' , device_map='auto' ) lowerCAmelCase__ : Any = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 lowerCAmelCase__ : Dict = torch.tensor([[-6.6_5_5_0, -4.1_2_2_7, -4.9_8_5_9, -3.2_4_0_6, 0.8_2_6_2, -3.0_0_3_3, 1.2_9_6_4, -3.3_6_9_9]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : List[Any] = torch.tensor([-1_2.8_2_8_1, -7.4_4_5_3, -0.4_6_3_9, -8.0_6_2_5, -7.2_5_0_0, -8.0_0_0_0, -6.4_8_8_3, -7.7_6_9_5, -7.8_4_3_8, -7.0_3_1_2, -6.2_1_8_8, -7.1_3_2_8, -1.8_4_9_6, 1.9_9_6_1, -8.6_2_5_0, -6.7_2_2_7, -1_2.8_2_8_1, -6.9_4_9_2, -7.0_7_4_2, -7.7_8_5_2, -7.5_8_2_0, -7.9_0_6_2, -6.9_3_7_5, -7.9_8_0_5, -8.3_4_3_8, -8.1_5_6_2, -8.0_4_6_9, -7.6_2_5_0, -7.7_4_2_2, -7.3_3_9_8,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : Optional[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' , device_map='auto' ) lowerCAmelCase__ : List[str] = model(torch.tensor(a ) ) # Expected mean on dim = -1 lowerCAmelCase__ : Union[str, Any] = torch.tensor([[-2.0_6_2_2, -1.2_7_9_4, -1.1_6_3_8, -0.9_7_8_8, -1.4_6_0_3, -1.0_2_3_8, -1.7_8_9_3, -1.4_4_1_1]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : Any = torch.tensor([-8.1_4_0_6, -8.0_5_4_7, 2.7_4_6_1, -1.2_3_4_4, -0.1_4_4_8, -1.8_2_6_2, -1.0_0_2_0, -1.8_1_5_4, -1.6_8_9_5, -1.8_5_1_6, -2.3_5_7_4, -0.9_2_7_7, 3.7_5_9_8, 6.5_7_4_2, -1.2_9_9_8, -0.1_1_7_7, -8.1_4_0_6, -2.9_6_8_8, -2.9_1_9_9, -3.1_6_9_9, -3.5_2_5_4, -2.3_5_5_5, -2.7_9_8_8, -3.4_1_4_1, -2.8_2_6_2, -4.5_1_9_5, -3.3_3_7_9, -3.3_1_6_4, -2.7_8_3_2, -3.0_2_7_3] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : int = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : Optional[int] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' , device_map='auto' ) lowerCAmelCase__ : str = model(torch.tensor(a ) ) # Expected mean on dim = -1 lowerCAmelCase__ : str = torch.tensor([[-0.8_5_6_2, -1.8_5_2_0, -0.7_5_5_1, -0.4_1_6_2, -1.5_1_6_1, -1.2_0_3_8, -2.4_8_2_3, -2.3_2_5_4]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : List[str] = torch.tensor([-2.2_2_2_7, 4.8_8_2_8, 0.9_0_2_3, -0.4_5_7_8, -0.7_8_7_1, -0.1_0_3_3, -0.6_2_2_1, -0.5_7_8_6, -0.7_8_0_3, -1.0_6_7_4, -1.2_9_2_0, -0.1_5_7_0, 0.8_0_0_8, 2.0_7_2_3, -0.9_4_9_7, 0.2_7_7_1, -2.2_2_2_7, -0.7_6_1_2, -1.4_3_4_6, -1.2_0_6_1, -1.6_4_2_6, -0.3_0_0_0, -0.7_1_3_9, -1.1_9_3_4, -1.8_6_9_1, -1.6_9_7_3, -1.5_9_4_7, -1.2_7_0_5, -0.3_5_2_3, -0.5_5_1_3] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' , device_map='auto' ) lowerCAmelCase__ : List[str] = model(torch.tensor(a ) ) lowerCAmelCase__ : int = torch.tensor( [[-4.2_3_2_7, -3.3_3_6_0, -4.6_6_6_5, -4.7_6_3_1, -1.8_1_8_0, -3.4_1_7_0, -1.4_2_1_1, -3.1_8_1_0]] , dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # fmt: off lowerCAmelCase__ : Optional[int] = torch.tensor([-9.4_9_2_2, -3.9_5_5_1, 1.7_9_9_8, -5.6_7_5_8, -5.1_0_5_5, -5.8_9_8_4, -4.8_3_2_0, -6.8_0_8_6, -6.5_3_9_1, -5.6_1_7_2, -5.5_8_2_0, -5.5_3_5_2, 1.7_8_8_1, 3.6_2_8_9, -6.5_1_1_7, -3.4_7_8_5, -9.5_0_0_0, -6.0_3_5_2, -6.8_1_2_5, -6.0_1_9_5, -6.6_8_3_6, -5.4_7_2_7, -6.2_8_1_2, -6.0_3_9_1, -7.3_3_9_8, -7.4_2_9_7, -7.4_8_4_4, -6.5_8_2_0, -5.8_7_8_9, -5.5_3_1_2] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' lowerCAmelCase__ : Tuple = 'Simply put, the theory of relativity states that ' lowerCAmelCase__ : Dict = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) lowerCAmelCase__ : Dict = tokenizer.encode(a , return_tensors='pt' ) lowerCAmelCase__ : str = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' , device_map='sequential' , use_safetensors=a ) # greedy generation outputs lowerCAmelCase__ : Optional[Any] = model.generate(a , max_new_tokens=64 , top_p=a , temperature=1 , do_sample=a ) lowerCAmelCase__ : Tuple = tokenizer.decode(generated_ids[0] , skip_special_tokens=a ) self.assertEqual(a , a )
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError('iterations must be defined as integers' ) if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or not number >= 1: raise ValueError( 'starting number must be\n and integer and be more than 0' ) if not iterations >= 1: raise ValueError('Iterations must be done more than 0 times to play FizzBuzz' ) lowerCAmelCase__ : Dict = """""" while number <= iterations: if number % 3 == 0: out += "Fizz" if number % 5 == 0: out += "Buzz" if 0 not in (number % 3, number % 5): out += str(UpperCAmelCase__ ) # print(out) number += 1 out += " " return out if __name__ == "__main__": import doctest doctest.testmod()
371
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """microsoft/unispeech-large-1500h-cv""": ( """https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json""" ), # See all UniSpeech models at https://huggingface.co/models?filter=unispeech } class A__ ( __magic_name__ ): lowercase = 'unispeech' def __init__( self : Any , a : List[Any]=32 , a : List[Any]=768 , a : Any=12 , a : List[str]=12 , a : List[Any]=3_072 , a : Any="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : List[str]=0.1 , a : Union[str, Any]=0.0 , a : str=0.0 , a : int=0.1 , a : List[str]=0.1 , a : List[Any]=0.0_2 , a : Optional[int]=1E-5 , a : Optional[int]="group" , a : Optional[Any]="gelu" , a : List[Any]=(512, 512, 512, 512, 512, 512, 512) , a : Optional[Any]=(5, 2, 2, 2, 2, 2, 2) , a : List[str]=(10, 3, 3, 3, 3, 2, 2) , a : Union[str, Any]=False , a : Union[str, Any]=128 , a : Tuple=16 , a : Dict=False , a : str=True , a : str=0.0_5 , a : Union[str, Any]=10 , a : Tuple=2 , a : int=0.0 , a : Optional[Any]=10 , a : List[str]=0 , a : str=320 , a : List[str]=2 , a : Optional[Any]=0.1 , a : Any=100 , a : Dict=256 , a : Any=256 , a : Dict=0.1 , a : List[Any]="mean" , a : Dict=False , a : str=False , a : Optional[int]=256 , a : Any=80 , a : List[Any]=0 , a : Optional[int]=1 , a : int=2 , a : List[Any]=0.5 , **a : int , ): '''simple docstring''' super().__init__(**a , pad_token_id=a , bos_token_id=a , eos_token_id=a ) lowerCAmelCase__ : List[str] = hidden_size lowerCAmelCase__ : List[str] = feat_extract_norm lowerCAmelCase__ : Optional[Any] = feat_extract_activation lowerCAmelCase__ : str = list(a ) lowerCAmelCase__ : List[str] = list(a ) lowerCAmelCase__ : Tuple = list(a ) lowerCAmelCase__ : Dict = conv_bias lowerCAmelCase__ : Optional[int] = num_conv_pos_embeddings lowerCAmelCase__ : Any = num_conv_pos_embedding_groups lowerCAmelCase__ : str = len(self.conv_dim ) lowerCAmelCase__ : Any = num_hidden_layers lowerCAmelCase__ : Dict = intermediate_size lowerCAmelCase__ : Dict = hidden_act lowerCAmelCase__ : Union[str, Any] = num_attention_heads lowerCAmelCase__ : Union[str, Any] = hidden_dropout lowerCAmelCase__ : Tuple = attention_dropout lowerCAmelCase__ : str = activation_dropout lowerCAmelCase__ : Any = feat_proj_dropout lowerCAmelCase__ : List[Any] = final_dropout lowerCAmelCase__ : Tuple = layerdrop lowerCAmelCase__ : Any = layer_norm_eps lowerCAmelCase__ : Dict = initializer_range lowerCAmelCase__ : Optional[Any] = num_ctc_classes lowerCAmelCase__ : Tuple = vocab_size lowerCAmelCase__ : Dict = do_stable_layer_norm lowerCAmelCase__ : List[Any] = use_weighted_layer_sum lowerCAmelCase__ : Any = classifier_proj_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)`, but is `len(config.conv_dim) =' f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCAmelCase__ : Union[str, Any] = apply_spec_augment lowerCAmelCase__ : Any = mask_time_prob lowerCAmelCase__ : Dict = mask_time_length lowerCAmelCase__ : Tuple = mask_time_min_masks lowerCAmelCase__ : Optional[int] = mask_feature_prob lowerCAmelCase__ : Optional[Any] = mask_feature_length lowerCAmelCase__ : int = mask_feature_min_masks # parameters for pretraining with codevector quantized representations lowerCAmelCase__ : int = num_codevectors_per_group lowerCAmelCase__ : Any = num_codevector_groups lowerCAmelCase__ : Any = contrastive_logits_temperature lowerCAmelCase__ : int = feat_quantizer_dropout lowerCAmelCase__ : List[Any] = num_negatives lowerCAmelCase__ : List[str] = codevector_dim lowerCAmelCase__ : Optional[int] = proj_codevector_dim lowerCAmelCase__ : Dict = diversity_loss_weight # ctc loss lowerCAmelCase__ : Any = ctc_loss_reduction lowerCAmelCase__ : Any = ctc_zero_infinity # pretraining loss lowerCAmelCase__ : Union[str, Any] = replace_prob @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
307
0
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 A__ ( __lowercase ): def __init__( self : Union[str, Any] , a : Tuple = None , a : str = None , a : Optional[int] = None , a : Dict = None , a : str = False , a : str = False , a : int = None , **a : Tuple , ): '''simple docstring''' lowerCAmelCase__ : Tuple = path_or_paths lowerCAmelCase__ : Dict = split if split or isinstance(_a , _a ) else 'train' lowerCAmelCase__ : List[Any] = features lowerCAmelCase__ : Union[str, Any] = cache_dir lowerCAmelCase__ : Optional[int] = keep_in_memory lowerCAmelCase__ : Dict = streaming lowerCAmelCase__ : str = num_proc lowerCAmelCase__ : List[str] = kwargs @abstractmethod def _lowerCamelCase ( self : List[str] ): '''simple docstring''' pass class A__ ( __lowercase ): def __init__( self : Union[str, Any] , a : List[Any] = None , a : Union[str, Any] = None , a : List[str] = False , a : Optional[Any] = False , a : Optional[Any] = None , **a : Dict , ): '''simple docstring''' lowerCAmelCase__ : Any = features lowerCAmelCase__ : int = cache_dir lowerCAmelCase__ : List[str] = keep_in_memory lowerCAmelCase__ : List[str] = streaming lowerCAmelCase__ : Tuple = num_proc lowerCAmelCase__ : int = kwargs @abstractmethod def _lowerCamelCase ( self : str ): '''simple docstring''' pass
350
import torch from torch import nn class A__ ( nn.Module ): def __init__( self : Optional[int] , a : Union[str, Any] , a : str , a : str , a : List[Any] , a : List[Any]=1 , a : Tuple=False ): '''simple docstring''' super().__init__() lowerCAmelCase__ : Dict = n_token lowerCAmelCase__ : Any = d_embed lowerCAmelCase__ : str = d_proj lowerCAmelCase__ : int = cutoffs + [n_token] lowerCAmelCase__ : Union[str, Any] = [0] + self.cutoffs lowerCAmelCase__ : str = div_val lowerCAmelCase__ : Tuple = self.cutoffs[0] lowerCAmelCase__ : Dict = len(self.cutoffs ) - 1 lowerCAmelCase__ : Any = self.shortlist_size + self.n_clusters if self.n_clusters > 0: lowerCAmelCase__ : int = nn.Parameter(torch.zeros(self.n_clusters , self.d_embed ) ) lowerCAmelCase__ : Optional[Any] = nn.Parameter(torch.zeros(self.n_clusters ) ) lowerCAmelCase__ : Optional[int] = nn.ModuleList() lowerCAmelCase__ : Tuple = nn.ParameterList() if div_val == 1: for i in range(len(self.cutoffs ) ): if d_proj != d_embed: self.out_projs.append(nn.Parameter(torch.FloatTensor(a , a ) ) ) else: self.out_projs.append(a ) self.out_layers.append(nn.Linear(a , a ) ) else: for i in range(len(self.cutoffs ) ): lowerCAmelCase__ , lowerCAmelCase__ : Any = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : Optional[Any] = d_embed // (div_val**i) self.out_projs.append(nn.Parameter(torch.FloatTensor(a , a ) ) ) self.out_layers.append(nn.Linear(a , r_idx - l_idx ) ) lowerCAmelCase__ : Tuple = keep_order def _lowerCamelCase ( self : Optional[int] , a : List[str] , a : int , a : List[str] , a : str ): '''simple docstring''' if proj is None: lowerCAmelCase__ : Tuple = nn.functional.linear(a , a , bias=a ) else: # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1: lowerCAmelCase__ : int = nn.functional.linear(a , proj.t().contiguous() ) lowerCAmelCase__ : Tuple = nn.functional.linear(a , a , bias=a ) # else: # logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t())) # if bias is not None: # logit = logit + bias return logit def _lowerCamelCase ( self : List[str] , a : List[Any] , a : Optional[int]=None , a : Tuple=False ): '''simple docstring''' if labels is not None: # Shift so that tokens < n predict n lowerCAmelCase__ : str = hidden[..., :-1, :].contiguous() lowerCAmelCase__ : Optional[Any] = labels[..., 1:].contiguous() lowerCAmelCase__ : List[Any] = hidden.view(-1 , hidden.size(-1 ) ) lowerCAmelCase__ : Tuple = labels.view(-1 ) if hidden.size(0 ) != labels.size(0 ): raise RuntimeError('Input and labels should have the same size in the batch dimension.' ) else: lowerCAmelCase__ : Optional[Any] = hidden.view(-1 , hidden.size(-1 ) ) if self.n_clusters == 0: lowerCAmelCase__ : Optional[Any] = self._compute_logit(a , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) if labels is not None: lowerCAmelCase__ : str = labels != -100 lowerCAmelCase__ : int = torch.zeros_like(a , dtype=hidden.dtype , device=hidden.device ) lowerCAmelCase__ : List[str] = ( -nn.functional.log_softmax(a , dim=-1 )[mask].gather(1 , labels[mask].unsqueeze(1 ) ).squeeze(1 ) ) else: lowerCAmelCase__ : Optional[Any] = nn.functional.log_softmax(a , dim=-1 ) else: # construct weights and biases lowerCAmelCase__ , lowerCAmelCase__ : int = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : Any = self.out_layers[0].weight[l_idx:r_idx] lowerCAmelCase__ : Any = self.out_layers[0].bias[l_idx:r_idx] else: lowerCAmelCase__ : Optional[Any] = self.out_layers[i].weight lowerCAmelCase__ : Optional[int] = self.out_layers[i].bias if i == 0: lowerCAmelCase__ : Dict = torch.cat([weight_i, self.cluster_weight] , dim=0 ) lowerCAmelCase__ : Union[str, Any] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(a ) biases.append(a ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = weights[0], biases[0], self.out_projs[0] lowerCAmelCase__ : List[Any] = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : Union[str, Any] = nn.functional.log_softmax(a , dim=1 ) if labels is None: lowerCAmelCase__ : Tuple = hidden.new_empty((head_logit.size(0 ), self.n_token) ) else: lowerCAmelCase__ : Dict = torch.zeros_like(a , dtype=hidden.dtype , device=hidden.device ) lowerCAmelCase__ : Tuple = 0 lowerCAmelCase__ : Union[str, Any] = [0] + self.cutoffs for i in range(len(a ) - 1 ): lowerCAmelCase__ , lowerCAmelCase__ : Tuple = cutoff_values[i], cutoff_values[i + 1] if labels is not None: lowerCAmelCase__ : Tuple = (labels >= l_idx) & (labels < r_idx) lowerCAmelCase__ : int = mask_i.nonzero().squeeze() if indices_i.numel() == 0: continue lowerCAmelCase__ : Tuple = labels.index_select(0 , a ) - l_idx lowerCAmelCase__ : Any = head_logprob.index_select(0 , a ) lowerCAmelCase__ : Optional[int] = hidden.index_select(0 , a ) else: lowerCAmelCase__ : Any = hidden if i == 0: if labels is not None: lowerCAmelCase__ : Union[str, Any] = head_logprob_i.gather(1 , target_i[:, None] ).squeeze(1 ) else: lowerCAmelCase__ : List[str] = head_logprob[:, : self.cutoffs[0]] else: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Any = weights[i], biases[i], self.out_projs[i] lowerCAmelCase__ : Union[str, Any] = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : Optional[int] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : List[Any] = self.cutoffs[0] + i - 1 # No probability for the head cluster if labels is not None: lowerCAmelCase__ : List[str] = head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather( 1 , target_i[:, None] ).squeeze(1 ) else: lowerCAmelCase__ : Tuple = head_logprob[:, cluster_prob_idx, None] + tail_logprob_i lowerCAmelCase__ : Union[str, Any] = logprob_i if labels is not None: if (hasattr(self , 'keep_order' ) and self.keep_order) or keep_order: out.index_copy_(0 , a , -logprob_i ) else: out[offset : offset + logprob_i.size(0 )].copy_(-logprob_i ) offset += logprob_i.size(0 ) return out def _lowerCamelCase ( self : List[Any] , a : Any ): '''simple docstring''' if self.n_clusters == 0: lowerCAmelCase__ : Union[str, Any] = self._compute_logit(a , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) return nn.functional.log_softmax(a , dim=-1 ) else: # construct weights and biases lowerCAmelCase__ , lowerCAmelCase__ : str = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : str = self.out_layers[0].weight[l_idx:r_idx] lowerCAmelCase__ : Dict = self.out_layers[0].bias[l_idx:r_idx] else: lowerCAmelCase__ : int = self.out_layers[i].weight lowerCAmelCase__ : int = self.out_layers[i].bias if i == 0: lowerCAmelCase__ : Optional[int] = torch.cat([weight_i, self.cluster_weight] , dim=0 ) lowerCAmelCase__ : Union[str, Any] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(a ) biases.append(a ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : str = weights[0], biases[0], self.out_projs[0] lowerCAmelCase__ : Dict = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : List[Any] = hidden.new_empty((head_logit.size(0 ), self.n_token) ) lowerCAmelCase__ : Optional[Any] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : List[Any] = [0] + self.cutoffs for i in range(len(a ) - 1 ): lowerCAmelCase__ , lowerCAmelCase__ : str = cutoff_values[i], cutoff_values[i + 1] if i == 0: lowerCAmelCase__ : Union[str, Any] = head_logprob[:, : self.cutoffs[0]] else: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = weights[i], biases[i], self.out_projs[i] lowerCAmelCase__ : Dict = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : List[str] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : Dict = head_logprob[:, -i] + tail_logprob_i lowerCAmelCase__ : List[str] = logprob_i return out
307
0
import os from datetime import datetime as dt from github import Github lowerCamelCase__ = [ """good first issue""", """feature request""", """wip""", ] def lowerCAmelCase__ ( ) -> Dict: lowerCAmelCase__ : Optional[Any] = Github(os.environ['GITHUB_TOKEN'] ) lowerCAmelCase__ : List[Any] = g.get_repo('huggingface/accelerate' ) lowerCAmelCase__ : Union[str, Any] = repo.get_issues(state='open' ) for issue in open_issues: lowerCAmelCase__ : Union[str, Any] = sorted([comment for comment in issue.get_comments()] , key=lambda SCREAMING_SNAKE_CASE_ : i.created_at , reverse=__lowerCamelCase ) lowerCAmelCase__ : Any = comments[0] if len(__lowerCamelCase ) > 0 else None lowerCAmelCase__ : Optional[Any] = dt.utcnow() lowerCAmelCase__ : Optional[int] = (current_time - issue.updated_at).days lowerCAmelCase__ : str = (current_time - issue.created_at).days if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and days_since_updated > 7 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Close issue since it has been 7 days of inactivity since bot mention. issue.edit(state='closed' ) elif ( days_since_updated > 23 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Add stale comment issue.create_comment( 'This issue has been automatically marked as stale because it has not had ' 'recent activity. If you think this still needs to be addressed ' 'please comment on this thread.\n\nPlease note that issues that do not follow the ' '[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) ' 'are likely to be ignored.' ) if __name__ == "__main__": main()
351
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate lowerCamelCase__ = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow("""""", """|""", """|"""), datarow=DataRow("""""", """|""", """|"""), padding=1, with_header_hide=None, ) lowerCamelCase__ = [] lowerCamelCase__ = [] lowerCamelCase__ = {"""type""": """section""", """text""": {"""type""": """plain_text""", """text""": """No failed tests! 🤗""", """emoji""": True}} lowerCamelCase__ = [ { """type""": """header""", """text""": { """type""": """plain_text""", """text""": F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", """emoji""": True, }, } ] lowerCamelCase__ = 0 for log in Path().glob("""*.log"""): lowerCamelCase__ = 0 with open(log, """r""") as f: for line in f: lowerCamelCase__ = json.loads(line) if line.get("""nodeid""", """""") != "": lowerCamelCase__ = line["""nodeid"""] if line.get("""duration""", None) is not None: lowerCamelCase__ = F"""{line["duration"]:.4f}""" if line.get("""outcome""", """""") == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split("""_""")[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) lowerCamelCase__ = [] log.unlink() lowerCamelCase__ = """""" lowerCamelCase__ = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" lowerCamelCase__ = [] lowerCamelCase__ = {} for test in failed_tests: lowerCamelCase__ = test[0].split("""::""") lowerCamelCase__ = data[0].split("""/""")[-1] if data[0] not in filesafailed: lowerCamelCase__ = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) lowerCamelCase__ = [test[0] for test in failed_table] lowerCamelCase__ = list(set(files)) # Count number of instances in failed_tests lowerCamelCase__ = [] for file in individual_files: table.append([file, len(filesafailed[file])]) lowerCamelCase__ = tabulate( table, headers=["""Test Location""", """Num Failed"""], tablefmt=hf_table_format, stralign="""right""", ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 3000: lowerCamelCase__ = """Too many failed tests, please see the full report in the Action results.""" lowerCamelCase__ = len(err) + 10 lowerCamelCase__ = message[: 3000 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: lowerCamelCase__ = """No failed tests! 🤗""" print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get("""TEST_TYPE""", """""") != "": from slack_sdk import WebClient lowerCamelCase__ = WebClient(token=os.environ["""SLACK_API_TOKEN"""]) if message != "No failed tests! 🤗": lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": message, }, } payload.append(md_report) lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": """*For more details:*""", }, """accessory""": { """type""": """button""", """text""": { """type""": """plain_text""", """text""": """Check Action results""", """emoji""": True, }, """url""": F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) lowerCamelCase__ = { """type""": """context""", """elements""": [ { """type""": """plain_text""", """text""": F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) lowerCamelCase__ = client.chat_postMessage(channel="""#accelerate-ci-daily""", text=message, blocks=payload) lowerCamelCase__ = response.data["""ts"""] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name lowerCamelCase__ = """""" for i, row in enumerate(test_failures): if row[0] != test_class: lowerCamelCase__ = row[0] else: lowerCamelCase__ = """""" lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel="""#accelerate-ci-daily""", thread_ts=ts, blocks=[payload], )
307
0
import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowerCamelCase__ = """0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> Tuple: if rng is None: lowerCAmelCase__ : str = random.Random() lowerCAmelCase__ : Optional[int] = 1 for dim in shape: total_dims *= dim lowerCAmelCase__ : Optional[int] = [] for _ in range(_snake_case ): values.append(rng.randint(0 , vocab_size - 1 ) ) lowerCAmelCase__ : List[str] = np.array(_snake_case , dtype=jnp.intaa ).reshape(_snake_case ) return output def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> Any: lowerCAmelCase__ : Optional[int] = ids_tensor(_snake_case , vocab_size=2 , rng=_snake_case ) # make sure that at least one token is attended to for each batch lowerCAmelCase__ : Optional[Any] = 1 return attn_mask @require_flax class A__ : lowercase = None lowercase = () def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 lowerCAmelCase__ : str = 2 lowerCAmelCase__ : Tuple = inputs['''input_ids'''].shape[-1] // 2 lowerCAmelCase__ : int = inputs['''input_ids'''][:max_batch_size, :sequence_length] lowerCAmelCase__ : int = jnp.ones_like(a_ ) lowerCAmelCase__ : Optional[int] = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens lowerCAmelCase__ : Dict = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` lowerCAmelCase__ : str = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Any = self._get_input_ids_and_config() lowerCAmelCase__ : Optional[Any] = False lowerCAmelCase__ : Tuple = max_length lowerCAmelCase__ : int = 0 for model_class in self.all_generative_model_classes: lowerCAmelCase__ : Optional[Any] = model_class(a_ ) lowerCAmelCase__ : Tuple = model_class.__name__[4:] # Skip the "Flax" at the beginning lowerCAmelCase__ : Any = getattr(a_ , a_ ) lowerCAmelCase__ : Optional[Any] = pt_model_class(a_ ).eval() lowerCAmelCase__ : Union[str, Any] = load_flax_weights_in_pytorch_model(a_ , flax_model.params ) lowerCAmelCase__ : Optional[Any] = flax_model.generate(a_ ).sequences lowerCAmelCase__ : Dict = pt_model.generate(torch.tensor(a_ , dtype=torch.long ) ) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: lowerCAmelCase__ : int = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist() ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = self._get_input_ids_and_config() lowerCAmelCase__ : Any = False lowerCAmelCase__ : Optional[Any] = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ : Dict = model_class(a_ ) lowerCAmelCase__ : Union[str, Any] = model.generate(a_ ).sequences self.assertEqual(generation_outputs.shape[-1] , a_ ) lowerCAmelCase__ : Tuple = jit(model.generate ) lowerCAmelCase__ : Tuple = jit_generate(a_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = self._get_input_ids_and_config() lowerCAmelCase__ : Dict = True lowerCAmelCase__ : Optional[Any] = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ : List[str] = model_class(a_ ) lowerCAmelCase__ : Tuple = model.generate(a_ ).sequences self.assertEqual(generation_outputs.shape[-1] , a_ ) lowerCAmelCase__ : Union[str, Any] = jit(model.generate ) lowerCAmelCase__ : List[str] = jit_generate(a_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : str = self._get_input_ids_and_config() lowerCAmelCase__ : List[str] = False lowerCAmelCase__ : List[Any] = max_length lowerCAmelCase__ : List[str] = 2 for model_class in self.all_generative_model_classes: lowerCAmelCase__ : Optional[int] = model_class(a_ ) lowerCAmelCase__ : str = model.generate(a_ ).sequences self.assertEqual(generation_outputs.shape[-1] , a_ ) lowerCAmelCase__ : List[Any] = jit(model.generate ) lowerCAmelCase__ : List[str] = jit_generate(a_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = self._get_input_ids_and_config() lowerCAmelCase__ : Optional[Any] = False lowerCAmelCase__ : Dict = max_length lowerCAmelCase__ : List[str] = 2 lowerCAmelCase__ : List[str] = 2 for model_class in self.all_generative_model_classes: lowerCAmelCase__ : Optional[Any] = model_class(a_ ) lowerCAmelCase__ : str = model.generate(a_ ).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self._get_input_ids_and_config() lowerCAmelCase__ : List[str] = True lowerCAmelCase__ : Tuple = max_length lowerCAmelCase__ : Optional[int] = 0.8 lowerCAmelCase__ : Dict = 10 lowerCAmelCase__ : Tuple = 0.3 lowerCAmelCase__ : List[Any] = 1 lowerCAmelCase__ : Tuple = 8 lowerCAmelCase__ : Optional[int] = 9 for model_class in self.all_generative_model_classes: lowerCAmelCase__ : Any = model_class(a_ ) lowerCAmelCase__ : Any = model.generate(a_ ).sequences self.assertEqual(generation_outputs.shape[-1] , a_ ) lowerCAmelCase__ : Any = jit(model.generate ) lowerCAmelCase__ : int = jit_generate(a_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = self._get_input_ids_and_config() lowerCAmelCase__ : Optional[int] = max_length lowerCAmelCase__ : int = 1 lowerCAmelCase__ : Dict = 8 lowerCAmelCase__ : Dict = 9 for model_class in self.all_generative_model_classes: lowerCAmelCase__ : Tuple = model_class(a_ ) lowerCAmelCase__ : Union[str, Any] = model.generate(a_ ).sequences self.assertEqual(generation_outputs.shape[-1] , a_ ) lowerCAmelCase__ : Optional[Any] = jit(model.generate ) lowerCAmelCase__ : Optional[Any] = jit_generate(a_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[str] = self._get_input_ids_and_config() lowerCAmelCase__ : Optional[int] = max_length lowerCAmelCase__ : Tuple = 2 lowerCAmelCase__ : Any = 1 lowerCAmelCase__ : Any = 8 lowerCAmelCase__ : Any = 9 for model_class in self.all_generative_model_classes: lowerCAmelCase__ : Optional[Any] = model_class(a_ ) lowerCAmelCase__ : Any = model.generate(a_ ).sequences self.assertEqual(generation_outputs.shape[-1] , a_ ) lowerCAmelCase__ : Optional[Any] = jit(model.generate ) lowerCAmelCase__ : Optional[int] = jit_generate(a_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Any = self._get_input_ids_and_config() # pad attention mask on the left lowerCAmelCase__ : Optional[int] = attention_mask.at[(0, 0)].set(0 ) lowerCAmelCase__ : str = False lowerCAmelCase__ : int = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ : Union[str, Any] = model_class(a_ ) lowerCAmelCase__ : int = model.generate(a_ , attention_mask=a_ ).sequences self.assertEqual(generation_outputs.shape[-1] , a_ ) lowerCAmelCase__ : int = jit(model.generate ) lowerCAmelCase__ : Tuple = jit_generate(a_ , attention_mask=a_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self._get_input_ids_and_config() # pad attention mask on the left lowerCAmelCase__ : Any = attention_mask.at[(0, 0)].set(0 ) lowerCAmelCase__ : Optional[Any] = True lowerCAmelCase__ : List[str] = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ : str = model_class(a_ ) lowerCAmelCase__ : Dict = model.generate(a_ , attention_mask=a_ ).sequences self.assertEqual(generation_outputs.shape[-1] , a_ ) lowerCAmelCase__ : Optional[int] = jit(model.generate ) lowerCAmelCase__ : List[Any] = jit_generate(a_ , attention_mask=a_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[Any] = self._get_input_ids_and_config() # pad attention mask on the left lowerCAmelCase__ : Any = attention_mask.at[(0, 0)].set(0 ) lowerCAmelCase__ : Union[str, Any] = 2 lowerCAmelCase__ : int = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ : List[str] = model_class(a_ ) lowerCAmelCase__ : Dict = model.generate(a_ , attention_mask=a_ ).sequences self.assertEqual(generation_outputs.shape[-1] , a_ ) lowerCAmelCase__ : Any = jit(model.generate ) lowerCAmelCase__ : Optional[int] = jit_generate(a_ , attention_mask=a_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) @require_flax class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-bert' ) lowerCAmelCase__ : int = FlaxAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-bert-flax-only' ) lowerCAmelCase__ : Union[str, Any] = '''Hello world''' lowerCAmelCase__ : Optional[Any] = tokenizer(a_ , return_tensors='np' ).input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(a_ , 'do_samples' ): model.generate(a_ , do_samples=a_ ) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(a_ , 'foo' ): lowerCAmelCase__ : Optional[int] = {'''foo''': '''bar'''} model.generate(a_ , **a_ )
352
import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs lowerCamelCase__ = imread(r"""digital_image_processing/image_data/lena_small.jpg""") lowerCamelCase__ = cvtColor(img, COLOR_BGR2GRAY) def lowerCAmelCase__ ( ) -> Dict: lowerCAmelCase__ : List[Any] = cn.convert_to_negative(SCREAMING_SNAKE_CASE_ ) # assert negative_img array for at least one True assert negative_img.any() def lowerCAmelCase__ ( ) -> Optional[Any]: with Image.open('digital_image_processing/image_data/lena_small.jpg' ) as img: # Work around assertion for response assert str(cc.change_contrast(SCREAMING_SNAKE_CASE_ , 110 ) ).startswith( '<PIL.Image.Image image mode=RGB size=100x100 at' ) def lowerCAmelCase__ ( ) -> Tuple: lowerCAmelCase__ : str = canny.gen_gaussian_kernel(9 , sigma=1.4 ) # Assert ambiguous array assert resp.all() def lowerCAmelCase__ ( ) -> Tuple: lowerCAmelCase__ : Tuple = imread('digital_image_processing/image_data/lena_small.jpg' , 0 ) # assert ambiguous array for all == True assert canny_img.all() lowerCAmelCase__ : Optional[Any] = canny.canny(SCREAMING_SNAKE_CASE_ ) # assert canny array for at least one True assert canny_array.any() def lowerCAmelCase__ ( ) -> Optional[int]: assert gg.gaussian_filter(SCREAMING_SNAKE_CASE_ , 5 , sigma=0.9 ).all() def lowerCAmelCase__ ( ) -> Dict: # laplace diagonals lowerCAmelCase__ : Union[str, Any] = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]] ) lowerCAmelCase__ : int = conv.img_convolve(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).astype(SCREAMING_SNAKE_CASE_ ) assert res.any() def lowerCAmelCase__ ( ) -> List[str]: assert med.median_filter(SCREAMING_SNAKE_CASE_ , 3 ).any() def lowerCAmelCase__ ( ) -> Any: lowerCAmelCase__ , lowerCAmelCase__ : str = sob.sobel_filter(SCREAMING_SNAKE_CASE_ ) assert grad.any() and theta.any() def lowerCAmelCase__ ( ) -> Any: lowerCAmelCase__ : int = sp.make_sepia(SCREAMING_SNAKE_CASE_ , 20 ) assert sepia.all() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = "digital_image_processing/image_data/lena_small.jpg" ) -> Optional[Any]: lowerCAmelCase__ : List[Any] = bs.Burkes(imread(SCREAMING_SNAKE_CASE_ , 1 ) , 120 ) burkes.process() assert burkes.output_img.any() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = "digital_image_processing/image_data/lena_small.jpg" , ) -> Any: lowerCAmelCase__ : Dict = rs.NearestNeighbour(imread(SCREAMING_SNAKE_CASE_ , 1 ) , 400 , 200 ) nn.process() assert nn.output.any() def lowerCAmelCase__ ( ) -> int: lowerCAmelCase__ : int = 'digital_image_processing/image_data/lena.jpg' # Reading the image and converting it to grayscale. lowerCAmelCase__ : List[str] = imread(SCREAMING_SNAKE_CASE_ , 0 ) # Test for get_neighbors_pixel function() return not None lowerCAmelCase__ : str = 0 lowerCAmelCase__ : str = 0 lowerCAmelCase__ : List[str] = image[x_coordinate][y_coordinate] lowerCAmelCase__ : Dict = lbp.get_neighbors_pixel( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image lowerCAmelCase__ : List[str] = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 , image.shape[0] ): for j in range(0 , image.shape[1] ): lowerCAmelCase__ : Dict = lbp.local_binary_value(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert lbp_image.any()
307
0
import re import string import numpy as np import datasets lowerCamelCase__ = """ Returns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list. """ lowerCamelCase__ = """ Args: predictions: List of predicted texts. references: List of reference texts. regexes_to_ignore: List, defaults to None. Regex expressions of characters to ignore when calculating the exact matches. Note: these regexes are removed from the input data before the changes based on the options below (e.g. ignore_case, ignore_punctuation, ignore_numbers) are applied. ignore_case: Boolean, defaults to False. If true, turns everything to lowercase so that capitalization differences are ignored. ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before comparing predictions and references. ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before comparing predictions and references. Returns: exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive. Examples: >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"] >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"] >>> results = exact_match.compute(references=refs, predictions=preds) >>> print(round(results[\"exact_match\"], 1)) 25.0 >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"] >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"] >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\"], ignore_case=True, ignore_punctuation=True) >>> print(round(results[\"exact_match\"], 1)) 50.0 >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"] >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"] >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True) >>> print(round(results[\"exact_match\"], 1)) 75.0 >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"] >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"] >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True) >>> print(round(results[\"exact_match\"], 1)) 100.0 >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"The cat sat on the mat.\", \"Theaters are great.\", \"It's like comparing oranges and apples.\"] >>> preds = [\"The cat sat on the mat?\", \"Theaters are great.\", \"It's like comparing apples and oranges.\"] >>> results = exact_match.compute(references=refs, predictions=preds) >>> print(round(results[\"exact_match\"], 1)) 33.3 """ lowerCamelCase__ = """ """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A__ ( datasets.Metric ): def _lowerCamelCase ( self : Dict ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Value('string' , id='sequence' ), } ) , reference_urls=[] , ) def _lowerCamelCase ( self : Optional[int] , a : Optional[int] , a : Optional[Any] , a : int=None , a : List[Any]=False , a : int=False , a : List[Any]=False , ): '''simple docstring''' if regexes_to_ignore is not None: for s in regexes_to_ignore: lowerCAmelCase__ : Optional[Any] = np.array([re.sub(snake_case__ , '' , snake_case__ ) for x in predictions] ) lowerCAmelCase__ : Tuple = np.array([re.sub(snake_case__ , '' , snake_case__ ) for x in references] ) else: lowerCAmelCase__ : str = np.asarray(snake_case__ ) lowerCAmelCase__ : Any = np.asarray(snake_case__ ) if ignore_case: lowerCAmelCase__ : Dict = np.char.lower(snake_case__ ) lowerCAmelCase__ : List[Any] = np.char.lower(snake_case__ ) if ignore_punctuation: lowerCAmelCase__ : Optional[Any] = string.punctuation.maketrans('' , '' , string.punctuation ) lowerCAmelCase__ : Dict = np.char.translate(snake_case__ , table=snake_case__ ) lowerCAmelCase__ : Tuple = np.char.translate(snake_case__ , table=snake_case__ ) if ignore_numbers: lowerCAmelCase__ : Optional[Any] = string.digits.maketrans('' , '' , string.digits ) lowerCAmelCase__ : Dict = np.char.translate(snake_case__ , table=snake_case__ ) lowerCAmelCase__ : int = np.char.translate(snake_case__ , table=snake_case__ ) lowerCAmelCase__ : int = predictions == references return {"exact_match": np.mean(snake_case__ ) * 100}
353
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCamelCase__ = { """configuration_blip""": [ """BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BlipConfig""", """BlipTextConfig""", """BlipVisionConfig""", ], """processing_blip""": ["""BlipProcessor"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""BlipImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """BlipModel""", """BlipPreTrainedModel""", """BlipForConditionalGeneration""", """BlipForQuestionAnswering""", """BlipVisionModel""", """BlipTextModel""", """BlipForImageTextRetrieval""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFBlipModel""", """TFBlipPreTrainedModel""", """TFBlipForConditionalGeneration""", """TFBlipForQuestionAnswering""", """TFBlipVisionModel""", """TFBlipTextModel""", """TFBlipForImageTextRetrieval""", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
# Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING, Dict, Optional import numpy as np import pyarrow as pa from .. import config from ..utils.logging import get_logger from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import jax import jaxlib lowerCamelCase__ = get_logger() lowerCamelCase__ = None class A__ ( TensorFormatter[Mapping, 'jax.Array', Mapping] ): def __init__( self : int , a : int=None , a : int=None , **a : Optional[int] ): '''simple docstring''' super().__init__(features=__UpperCAmelCase ) import jax from jaxlib.xla_client import Device if isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise ValueError( f'''Expected {device} to be a `str` not {type(__UpperCAmelCase )}, as `jaxlib.xla_extension.Device` ''' 'is not serializable neither with `pickle` nor with `dill`. Instead you can surround ' 'the device with `str()` to get its string identifier that will be internally mapped ' 'to the actual `jaxlib.xla_extension.Device`.' ) lowerCAmelCase__ : Union[str, Any] = device if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else str(jax.devices()[0] ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: lowerCAmelCase__ : List[Any] = self._map_devices_to_str() if self.device not in list(DEVICE_MAPPING.keys() ): logger.warning( f'''Device with string identifier {self.device} not listed among the available ''' f'''devices: {list(DEVICE_MAPPING.keys() )}, so falling back to the default ''' f'''device: {str(jax.devices()[0] )}.''' ) lowerCAmelCase__ : Optional[Any] = str(jax.devices()[0] ) lowerCAmelCase__ : List[str] = jnp_array_kwargs @staticmethod def _lowerCamelCase ( ): '''simple docstring''' import jax return {str(__UpperCAmelCase ): device for device in jax.devices()} def _lowerCamelCase ( self : List[Any] , a : Union[str, Any] ): '''simple docstring''' import jax import jax.numpy as jnp if isinstance(__UpperCAmelCase , __UpperCAmelCase ) and column: if all( isinstance(__UpperCAmelCase , jax.Array ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return jnp.stack(__UpperCAmelCase , axis=0 ) return column def _lowerCamelCase ( self : Dict , a : Optional[int] ): '''simple docstring''' import jax import jax.numpy as jnp if isinstance(__UpperCAmelCase , (str, bytes, type(__UpperCAmelCase )) ): return value elif isinstance(__UpperCAmelCase , (np.character, np.ndarray) ) and np.issubdtype(value.dtype , np.character ): return value.tolist() lowerCAmelCase__ : List[Any] = {} if isinstance(__UpperCAmelCase , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.integer ): # the default int precision depends on the jax config # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision if jax.config.jax_enable_xaa: lowerCAmelCase__ : int = {'dtype': jnp.intaa} else: lowerCAmelCase__ : str = {'dtype': jnp.intaa} elif isinstance(__UpperCAmelCase , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.floating ): lowerCAmelCase__ : Optional[Any] = {'dtype': jnp.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(__UpperCAmelCase , PIL.Image.Image ): lowerCAmelCase__ : str = np.asarray(__UpperCAmelCase ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: lowerCAmelCase__ : Union[str, Any] = self._map_devices_to_str() with jax.default_device(DEVICE_MAPPING[self.device] ): # calling jnp.array on a np.ndarray does copy the data # see https://github.com/google/jax/issues/4486 return jnp.array(__UpperCAmelCase , **{**default_dtype, **self.jnp_array_kwargs} ) def _lowerCamelCase ( self : Optional[int] , a : Optional[Any] ): '''simple docstring''' import jax # support for torch, tf, jax etc. if config.TORCH_AVAILABLE and "torch" in sys.modules: import torch if isinstance(__UpperCAmelCase , torch.Tensor ): return self._tensorize(data_struct.detach().cpu().numpy()[()] ) if hasattr(__UpperCAmelCase , '__array__' ) and not isinstance(__UpperCAmelCase , jax.Array ): lowerCAmelCase__ : List[str] = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(__UpperCAmelCase , np.ndarray ): if data_struct.dtype == object: # jax arrays cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(__UpperCAmelCase ) for substruct in data_struct] ) elif isinstance(__UpperCAmelCase , (list, tuple) ): return self._consolidate([self.recursive_tensorize(__UpperCAmelCase ) for substruct in data_struct] ) return self._tensorize(__UpperCAmelCase ) def _lowerCamelCase ( self : Optional[int] , a : dict ): '''simple docstring''' return map_nested(self._recursive_tensorize , __UpperCAmelCase , map_list=__UpperCAmelCase ) def _lowerCamelCase ( self : str , a : pa.Table ): '''simple docstring''' lowerCAmelCase__ : Any = self.numpy_arrow_extractor().extract_row(__UpperCAmelCase ) lowerCAmelCase__ : Union[str, Any] = self.python_features_decoder.decode_row(__UpperCAmelCase ) return self.recursive_tensorize(__UpperCAmelCase ) def _lowerCamelCase ( self : int , a : pa.Table ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.numpy_arrow_extractor().extract_column(__UpperCAmelCase ) lowerCAmelCase__ : Tuple = self.python_features_decoder.decode_column(__UpperCAmelCase , pa_table.column_names[0] ) lowerCAmelCase__ : Optional[int] = self.recursive_tensorize(__UpperCAmelCase ) lowerCAmelCase__ : Optional[Any] = self._consolidate(__UpperCAmelCase ) return column def _lowerCamelCase ( self : Union[str, Any] , a : pa.Table ): '''simple docstring''' lowerCAmelCase__ : int = self.numpy_arrow_extractor().extract_batch(__UpperCAmelCase ) lowerCAmelCase__ : Optional[int] = self.python_features_decoder.decode_batch(__UpperCAmelCase ) lowerCAmelCase__ : Optional[Any] = self.recursive_tensorize(__UpperCAmelCase ) for column_name in batch: lowerCAmelCase__ : Any = self._consolidate(batch[column_name] ) return batch
354
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=0.999 , SCREAMING_SNAKE_CASE_="cosine" , ) -> Union[str, Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(SCREAMING_SNAKE_CASE_ ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(SCREAMING_SNAKE_CASE_ ): return math.exp(t * -12.0 ) else: raise ValueError(F'''Unsupported alpha_tranform_type: {alpha_transform_type}''' ) lowerCAmelCase__ : Tuple = [] for i in range(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : List[Any] = i / num_diffusion_timesteps lowerCAmelCase__ : str = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) return torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.floataa ) class A__ ( __magic_name__ , __magic_name__ ): lowercase = [e.name for e in KarrasDiffusionSchedulers] lowercase = 2 @register_to_config def __init__( self : Union[str, Any] , a : int = 1_000 , a : float = 0.0_0_0_8_5 , a : float = 0.0_1_2 , a : str = "linear" , a : Optional[Union[np.ndarray, List[float]]] = None , a : str = "epsilon" , a : Optional[bool] = False , a : Optional[bool] = False , a : float = 1.0 , a : str = "linspace" , a : int = 0 , ): '''simple docstring''' if trained_betas is not None: lowerCAmelCase__ : List[str] = torch.tensor(a , dtype=torch.floataa ) elif beta_schedule == "linear": lowerCAmelCase__ : List[str] = torch.linspace(a , a , a , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowerCAmelCase__ : Union[str, Any] = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , a , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowerCAmelCase__ : int = betas_for_alpha_bar(a , alpha_transform_type='cosine' ) elif beta_schedule == "exp": lowerCAmelCase__ : List[str] = betas_for_alpha_bar(a , alpha_transform_type='exp' ) else: raise NotImplementedError(f'''{beta_schedule} does is not implemented for {self.__class__}''' ) lowerCAmelCase__ : int = 1.0 - self.betas lowerCAmelCase__ : Tuple = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(a , a , a ) lowerCAmelCase__ : Optional[Any] = use_karras_sigmas def _lowerCamelCase ( self : str , a : List[Any] , a : str=None ): '''simple docstring''' if schedule_timesteps is None: lowerCAmelCase__ : List[str] = self.timesteps lowerCAmelCase__ : int = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: lowerCAmelCase__ : List[str] = 1 if len(a ) > 1 else 0 else: lowerCAmelCase__ : Tuple = timestep.cpu().item() if torch.is_tensor(a ) else timestep lowerCAmelCase__ : Tuple = self._index_counter[timestep_int] return indices[pos].item() @property def _lowerCamelCase ( self : Dict ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _lowerCamelCase ( self : Tuple , a : torch.FloatTensor , a : Union[float, torch.FloatTensor] , ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.index_for_timestep(a ) lowerCAmelCase__ : Any = self.sigmas[step_index] lowerCAmelCase__ : Optional[Any] = sample / ((sigma**2 + 1) ** 0.5) return sample def _lowerCamelCase ( self : List[str] , a : int , a : Union[str, torch.device] = None , a : Optional[int] = None , ): '''simple docstring''' lowerCAmelCase__ : Any = num_inference_steps lowerCAmelCase__ : Union[str, Any] = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": lowerCAmelCase__ : Union[str, Any] = np.linspace(0 , num_train_timesteps - 1 , a , dtype=a )[::-1].copy() elif self.config.timestep_spacing == "leading": lowerCAmelCase__ : List[Any] = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : Dict = (np.arange(0 , a ) * step_ratio).round()[::-1].copy().astype(a ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": lowerCAmelCase__ : Tuple = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : int = (np.arange(a , 0 , -step_ratio )).round().copy().astype(a ) timesteps -= 1 else: raise ValueError( f'''{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.''' ) lowerCAmelCase__ : str = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) lowerCAmelCase__ : List[Any] = np.log(a ) lowerCAmelCase__ : Optional[int] = np.interp(a , np.arange(0 , len(a ) ) , a ) if self.config.use_karras_sigmas: lowerCAmelCase__ : str = self._convert_to_karras(in_sigmas=a , num_inference_steps=self.num_inference_steps ) lowerCAmelCase__ : Union[str, Any] = np.array([self._sigma_to_t(a , a ) for sigma in sigmas] ) lowerCAmelCase__ : Tuple = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) lowerCAmelCase__ : Dict = torch.from_numpy(a ).to(device=a ) lowerCAmelCase__ : Union[str, Any] = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) lowerCAmelCase__ : Tuple = torch.from_numpy(a ) lowerCAmelCase__ : List[str] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(a ).startswith('mps' ): # mps does not support float64 lowerCAmelCase__ : Optional[Any] = timesteps.to(a , dtype=torch.floataa ) else: lowerCAmelCase__ : Any = timesteps.to(device=a ) # empty dt and derivative lowerCAmelCase__ : str = None lowerCAmelCase__ : Optional[int] = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter lowerCAmelCase__ : Optional[Any] = defaultdict(a ) def _lowerCamelCase ( self : Any , a : Dict , a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.log(a ) # get distribution lowerCAmelCase__ : Tuple = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range lowerCAmelCase__ : Optional[int] = np.cumsum((dists >= 0) , axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) lowerCAmelCase__ : List[str] = low_idx + 1 lowerCAmelCase__ : List[str] = log_sigmas[low_idx] lowerCAmelCase__ : Any = log_sigmas[high_idx] # interpolate sigmas lowerCAmelCase__ : Union[str, Any] = (low - log_sigma) / (low - high) lowerCAmelCase__ : List[Any] = np.clip(a , 0 , 1 ) # transform interpolation to time range lowerCAmelCase__ : List[Any] = (1 - w) * low_idx + w * high_idx lowerCAmelCase__ : Any = t.reshape(sigma.shape ) return t def _lowerCamelCase ( self : Tuple , a : torch.FloatTensor , a : Any ): '''simple docstring''' lowerCAmelCase__ : float = in_sigmas[-1].item() lowerCAmelCase__ : float = in_sigmas[0].item() lowerCAmelCase__ : Tuple = 7.0 # 7.0 is the value used in the paper lowerCAmelCase__ : Tuple = np.linspace(0 , 1 , a ) lowerCAmelCase__ : Any = sigma_min ** (1 / rho) lowerCAmelCase__ : Optional[Any] = sigma_max ** (1 / rho) lowerCAmelCase__ : Tuple = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def _lowerCamelCase ( self : Any ): '''simple docstring''' return self.dt is None def _lowerCamelCase ( self : List[str] , a : Union[torch.FloatTensor, np.ndarray] , a : Union[float, torch.FloatTensor] , a : Union[torch.FloatTensor, np.ndarray] , a : bool = True , ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.index_for_timestep(a ) # advance index counter by 1 lowerCAmelCase__ : Tuple = timestep.cpu().item() if torch.is_tensor(a ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: lowerCAmelCase__ : Union[str, Any] = self.sigmas[step_index] lowerCAmelCase__ : Union[str, Any] = self.sigmas[step_index + 1] else: # 2nd order / Heun's method lowerCAmelCase__ : int = self.sigmas[step_index - 1] lowerCAmelCase__ : Any = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API lowerCAmelCase__ : Optional[int] = 0 lowerCAmelCase__ : Union[str, Any] = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": lowerCAmelCase__ : int = sigma_hat if self.state_in_first_order else sigma_next lowerCAmelCase__ : Any = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase__ : Dict = sigma_hat if self.state_in_first_order else sigma_next lowerCAmelCase__ : List[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": lowerCAmelCase__ : int = model_output else: raise ValueError( f'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`''' ) if self.config.clip_sample: lowerCAmelCase__ : str = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order lowerCAmelCase__ : Dict = (sample - pred_original_sample) / sigma_hat # 3. delta timestep lowerCAmelCase__ : Optional[int] = sigma_next - sigma_hat # store for 2nd order step lowerCAmelCase__ : List[Any] = derivative lowerCAmelCase__ : str = dt lowerCAmelCase__ : Dict = sample else: # 2. 2nd order / Heun's method lowerCAmelCase__ : Union[str, Any] = (sample - pred_original_sample) / sigma_next lowerCAmelCase__ : Union[str, Any] = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample lowerCAmelCase__ : Dict = self.dt lowerCAmelCase__ : Optional[int] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" lowerCAmelCase__ : List[str] = None lowerCAmelCase__ : Tuple = None lowerCAmelCase__ : str = None lowerCAmelCase__ : Tuple = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=a ) def _lowerCamelCase ( self : int , a : torch.FloatTensor , a : torch.FloatTensor , a : torch.FloatTensor , ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(a ): # mps does not support float64 lowerCAmelCase__ : Optional[int] = self.timesteps.to(original_samples.device , dtype=torch.floataa ) lowerCAmelCase__ : int = timesteps.to(original_samples.device , dtype=torch.floataa ) else: lowerCAmelCase__ : Union[str, Any] = self.timesteps.to(original_samples.device ) lowerCAmelCase__ : Optional[Any] = timesteps.to(original_samples.device ) lowerCAmelCase__ : List[Any] = [self.index_for_timestep(a , a ) for t in timesteps] lowerCAmelCase__ : List[str] = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): lowerCAmelCase__ : Any = sigma.unsqueeze(-1 ) lowerCAmelCase__ : List[str] = original_samples + noise * sigma return noisy_samples def __len__( self : int ): '''simple docstring''' return self.config.num_train_timesteps
307
0
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import MaskaFormerConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel if is_vision_available(): from transformers import MaskaFormerImageProcessor if is_vision_available(): from PIL import Image class A__ : def __init__( self : List[str] , a : Any , a : Union[str, Any]=2 , a : Any=True , a : Union[str, Any]=False , a : Union[str, Any]=10 , a : List[Any]=3 , a : Union[str, Any]=32 * 8 , a : Optional[Any]=32 * 8 , a : Tuple=4 , a : int=64 , ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = parent lowerCAmelCase__ : str = batch_size lowerCAmelCase__ : str = is_training lowerCAmelCase__ : List[str] = use_auxiliary_loss lowerCAmelCase__ : List[str] = num_queries lowerCAmelCase__ : str = num_channels lowerCAmelCase__ : Optional[Any] = min_size lowerCAmelCase__ : Optional[int] = max_size lowerCAmelCase__ : Any = num_labels lowerCAmelCase__ : Optional[int] = hidden_dim lowerCAmelCase__ : str = hidden_dim def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : List[str] = torch.ones([self.batch_size, self.min_size, self.max_size] , device=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : int = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=SCREAMING_SNAKE_CASE_ ) > 0.5 ).float() lowerCAmelCase__ : Optional[Any] = (torch.rand((self.batch_size, self.num_labels) , device=SCREAMING_SNAKE_CASE_ ) > 0.5).long() lowerCAmelCase__ : Union[str, Any] = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : str = MaskaFormerConfig( hidden_size=self.hidden_dim , ) lowerCAmelCase__ : int = self.num_queries lowerCAmelCase__ : int = self.num_labels lowerCAmelCase__ : Union[str, Any] = [1, 1, 1, 1] lowerCAmelCase__ : Any = self.num_channels lowerCAmelCase__ : Union[str, Any] = 64 lowerCAmelCase__ : Optional[int] = 128 lowerCAmelCase__ : Optional[Any] = self.hidden_dim lowerCAmelCase__ : List[str] = self.hidden_dim lowerCAmelCase__ : Any = self.hidden_dim return config def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : List[Any] = self.prepare_config_and_inputs() lowerCAmelCase__ : Optional[Any] = {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask} return config, inputs_dict def _lowerCamelCase ( self : List[str] , a : Tuple , a : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = output.encoder_hidden_states lowerCAmelCase__ : List[str] = output.pixel_decoder_hidden_states lowerCAmelCase__ : Dict = output.transformer_decoder_hidden_states self.parent.assertTrue(len(SCREAMING_SNAKE_CASE_ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(SCREAMING_SNAKE_CASE_ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(SCREAMING_SNAKE_CASE_ ) , config.decoder_layers ) def _lowerCamelCase ( self : List[Any] , a : Optional[int] , a : int , a : Optional[int] , a : str=False ): '''simple docstring''' with torch.no_grad(): lowerCAmelCase__ : Dict = MaskaFormerModel(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase__ : str = model(pixel_values=SCREAMING_SNAKE_CASE_ , pixel_mask=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[Any] = model(SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ ) 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(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _lowerCamelCase ( self : Dict , a : Optional[Any] , a : Optional[Any] , a : Dict , a : Union[str, Any] , a : Dict ): '''simple docstring''' lowerCAmelCase__ : Dict = MaskaFormerForUniversalSegmentation(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() def comm_check_on_output(a : List[Any] ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): lowerCAmelCase__ : Optional[Any] = model(pixel_values=SCREAMING_SNAKE_CASE_ , pixel_mask=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : int = model(SCREAMING_SNAKE_CASE_ ) comm_check_on_output(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : int = model( pixel_values=SCREAMING_SNAKE_CASE_ , pixel_mask=SCREAMING_SNAKE_CASE_ , mask_labels=SCREAMING_SNAKE_CASE_ , class_labels=SCREAMING_SNAKE_CASE_ ) comm_check_on_output(SCREAMING_SNAKE_CASE_ ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class A__ ( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): lowercase = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () lowercase = {'feature-extraction': MaskaFormerModel} if is_torch_available() else {} lowercase = False lowercase = False lowercase = False lowercase = False def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = MaskaFormerModelTester(self ) lowerCAmelCase__ : List[str] = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , has_text_modality=SCREAMING_SNAKE_CASE_ ) def _lowerCamelCase ( self : Any ): '''simple docstring''' self.config_tester.run_common_tests() def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ ) def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*SCREAMING_SNAKE_CASE_ ) @unittest.skip(reason='Mask2Former does not use inputs_embeds' ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' pass @unittest.skip(reason='Mask2Former does not have a get_input_embeddings method' ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' pass @unittest.skip(reason='Mask2Former is not a generative model' ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' pass @unittest.skip(reason='Mask2Former does not use token embeddings' ) def _lowerCamelCase ( self : Union[str, Any] ): '''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 _lowerCamelCase ( self : int ): '''simple docstring''' pass @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase__ : int = model_class(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase__ : int = [*signature.parameters.keys()] lowerCAmelCase__ : List[Any] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , SCREAMING_SNAKE_CASE_ ) @slow def _lowerCamelCase ( self : List[str] ): '''simple docstring''' for model_name in ["facebook/mask2former-swin-small-coco-instance"]: lowerCAmelCase__ : str = MaskaFormerModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = (self.model_tester.min_size,) * 2 lowerCAmelCase__ : Any = { """pixel_values""": torch.randn((2, 3, *size) , device=SCREAMING_SNAKE_CASE_ ), """mask_labels""": torch.randn((2, 10, *size) , device=SCREAMING_SNAKE_CASE_ ), """class_labels""": torch.zeros(2 , 10 , device=SCREAMING_SNAKE_CASE_ ).long(), } lowerCAmelCase__ : Optional[Any] = self.model_tester.get_config() lowerCAmelCase__ : List[str] = MaskaFormerForUniversalSegmentation(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : int = model(**SCREAMING_SNAKE_CASE_ ) self.assertTrue(outputs.loss is not None ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : int = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase__ : List[str] = model_class(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : str = model(**SCREAMING_SNAKE_CASE_ , output_attentions=SCREAMING_SNAKE_CASE_ ) self.assertTrue(outputs.attentions is not None ) def _lowerCamelCase ( self : Any ): '''simple docstring''' if not self.model_tester.is_training: return lowerCAmelCase__ : int = self.all_model_classes[1] lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Optional[int] = model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.train() lowerCAmelCase__ : str = model(SCREAMING_SNAKE_CASE_ , mask_labels=SCREAMING_SNAKE_CASE_ , class_labels=SCREAMING_SNAKE_CASE_ ).loss loss.backward() def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.all_model_classes[1] lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Optional[int] = True lowerCAmelCase__ : int = True lowerCAmelCase__ : Any = model_class(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) model.train() lowerCAmelCase__ : Dict = model(SCREAMING_SNAKE_CASE_ , mask_labels=SCREAMING_SNAKE_CASE_ , class_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : str = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() lowerCAmelCase__ : List[str] = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() lowerCAmelCase__ : List[str] = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() lowerCAmelCase__ : Dict = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) lowerCamelCase__ = 1E-4 def lowerCAmelCase__ ( ) -> Dict: lowerCAmelCase__ : Union[str, Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_vision @slow class A__ ( unittest.TestCase ): @cached_property def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return "facebook/mask2former-swin-small-coco-instance" @cached_property def _lowerCamelCase ( self : int ): '''simple docstring''' return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints ) if is_vision_available() else None def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = MaskaFormerModel.from_pretrained(self.model_checkpoints ).to(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = self.default_image_processor lowerCAmelCase__ : str = prepare_img() lowerCAmelCase__ : Tuple = image_processor(SCREAMING_SNAKE_CASE_ , return_tensors='pt' ).to(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[Any] = 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(SCREAMING_SNAKE_CASE_ , (1, 3, 384, 384) ) with torch.no_grad(): lowerCAmelCase__ : Union[str, Any] = model(**SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Union[str, Any] = torch.tensor( [[-0.2_7_9_0, -1.0_7_1_7, -1.1_6_6_8], [-0.5_1_2_8, -0.3_1_2_8, -0.4_9_8_7], [-0.5_8_3_2, 0.1_9_7_1, -0.0_1_9_7]] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase__ : Union[str, Any] = torch.tensor( [[0.8_9_7_3, 1.1_8_4_7, 1.1_7_7_6], [1.1_9_3_4, 1.5_0_4_0, 1.5_1_2_8], [1.1_1_5_3, 1.4_4_8_6, 1.4_9_5_1]] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase__ : List[Any] = torch.tensor( [[2.1_1_5_2, 1.7_0_0_0, -0.8_6_0_3], [1.5_8_0_8, 1.8_0_0_4, -0.9_3_5_3], [1.6_0_4_3, 1.7_4_9_5, -0.5_9_9_9]] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(SCREAMING_SNAKE_CASE_ ).eval() lowerCAmelCase__ : List[str] = self.default_image_processor lowerCAmelCase__ : List[Any] = prepare_img() lowerCAmelCase__ : List[str] = image_processor(SCREAMING_SNAKE_CASE_ , return_tensors='pt' ).to(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = 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(SCREAMING_SNAKE_CASE_ , (1, 3, 384, 384) ) with torch.no_grad(): lowerCAmelCase__ : Union[str, Any] = model(**SCREAMING_SNAKE_CASE_ ) # masks_queries_logits lowerCAmelCase__ : str = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ) lowerCAmelCase__ : Dict = [ [-8.7_8_3_9, -9.0_0_5_6, -8.8_1_2_1], [-7.4_1_0_4, -7.0_3_1_3, -6.5_4_0_1], [-6.6_1_0_5, -6.3_4_2_7, -6.4_6_7_5], ] lowerCAmelCase__ : Dict = torch.tensor(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) ) # class_queries_logits lowerCAmelCase__ : Tuple = outputs.class_queries_logits self.assertEqual(class_queries_logits.shape , (1, model.config.num_queries, model.config.num_labels + 1) ) lowerCAmelCase__ : Dict = torch.tensor( [ [1.8_3_2_4, -8.0_8_3_5, -4.1_9_2_2], [0.8_4_5_0, -9.0_0_5_0, -3.6_0_5_3], [0.3_0_4_5, -7.7_2_9_3, -3.0_2_7_5], ] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(SCREAMING_SNAKE_CASE_ ).eval() lowerCAmelCase__ : Union[str, Any] = self.default_image_processor lowerCAmelCase__ : Union[str, Any] = 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__ : Optional[int] = inputs["""pixel_values"""].to(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Union[str, Any] = [el.to(SCREAMING_SNAKE_CASE_ ) for el in inputs["""mask_labels"""]] lowerCAmelCase__ : Tuple = [el.to(SCREAMING_SNAKE_CASE_ ) for el in inputs["""class_labels"""]] with torch.no_grad(): lowerCAmelCase__ : Dict = model(**SCREAMING_SNAKE_CASE_ ) self.assertTrue(outputs.loss is not None )
355
from __future__ import annotations import collections import tempfile import unittest import numpy as np from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import is_tf_available, is_vision_available from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_tf_bert import TFBertModelTester from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester from ..deit.test_modeling_tf_deit import TFDeiTModelTester from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester from ..vit.test_modeling_tf_vit import TFViTModelTester if is_tf_available(): from transformers import ( TFBertModel, TFCLIPVisionModel, TFDeiTModel, TFRobertaModel, TFVisionTextDualEncoderModel, TFViTModel, VisionTextDualEncoderConfig, ) if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: if isinstance(SCREAMING_SNAKE_CASE_ , collections.abc.Iterable ): return x return (x, x) @require_tf class A__ : def _lowerCamelCase ( self : List[Any] , a : List[str] , a : Optional[Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : Dict ): '''simple docstring''' pass def _lowerCamelCase ( self : Dict , a : int , a : str , a : List[Any] , a : Dict , a : List[str]=None , **a : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = VisionTextDualEncoderConfig.from_vision_text_configs(a , a ) lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel(a ) lowerCAmelCase__ : Tuple = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], config.projection_dim) ) def _lowerCamelCase ( self : Union[str, Any] , a : Dict , a : Tuple , a : Dict , a : Union[str, Any] , a : List[Any]=None , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.get_vision_text_model(a , a ) lowerCAmelCase__ : List[Any] = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : Optional[int] = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def _lowerCamelCase ( self : List[str] , a : Optional[int] , a : Optional[int] , a : Union[str, Any] , a : List[Any] , a : Any=None , **a : Dict ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Optional[Any] = {'vision_model': vision_model, 'text_model': text_model} lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel.from_vision_text_pretrained(**a ) lowerCAmelCase__ : Union[str, Any] = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def _lowerCamelCase ( self : Any , a : Optional[int] , a : Optional[int] , a : Dict , a : Optional[int] , a : Optional[int]=None , **a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : int = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Dict = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : List[str] = model(input_ids=a , pixel_values=a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = output[0].numpy() with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(a ) lowerCAmelCase__ : Any = TFVisionTextDualEncoderModel.from_pretrained(a ) lowerCAmelCase__ : int = model(input_ids=a , pixel_values=a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = after_output[0].numpy() lowerCAmelCase__ : Optional[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(a , 1E-5 ) def _lowerCamelCase ( self : List[str] , a : Dict , a : Optional[int] , a : List[Any] , a : str , a : int=None , **a : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Any = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : str = model( input_ids=a , pixel_values=a , attention_mask=a , output_attentions=a ) lowerCAmelCase__ : Union[str, Any] = output.vision_model_output.attentions self.assertEqual(len(a ) , vision_config.num_hidden_layers ) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCAmelCase__ : Optional[int] = to_atuple(vision_model.config.image_size ) lowerCAmelCase__ : Optional[Any] = to_atuple(vision_model.config.patch_size ) lowerCAmelCase__ : List[Any] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase__ : int = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCAmelCase__ : str = output.text_model_output.attentions self.assertEqual(len(a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _lowerCamelCase ( self : List[Any] , a : np.ndarray , a : np.ndarray , a : float ): '''simple docstring''' lowerCAmelCase__ : int = np.abs((a - b) ).max() self.assertLessEqual(a , a , f'''Difference between torch and flax is {diff} (>= {tol}).''' ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_model(**a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Any = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : str = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.prepare_config_and_inputs() self.check_save_load(**a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**a ) @slow def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = self.get_pretrained_model_and_inputs() lowerCAmelCase__ : List[Any] = model_a(**a ) lowerCAmelCase__ : Optional[int] = outputs[0].numpy() with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(a ) lowerCAmelCase__ : str = TFVisionTextDualEncoderModel.from_pretrained(a ) lowerCAmelCase__ : List[str] = model_a(**a ) lowerCAmelCase__ : int = after_outputs[0].numpy() lowerCAmelCase__ : List[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(a , 1E-5 ) @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-vit' , 'hf-internal-testing/tiny-random-bert' ) lowerCAmelCase__ : int = 13 lowerCAmelCase__ : List[Any] = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : int = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Optional[Any] = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : List[Any] = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : List[Any] , a : Dict , a : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = TFViTModel(a , name='vision_model' ) lowerCAmelCase__ : str = TFBertModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = TFViTModelTester(self ) lowerCAmelCase__ : Tuple = TFBertModelTester(self ) lowerCAmelCase__ : Optional[int] = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Union[str, Any] = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : int = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : str = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'Rocketknight1/tiny-random-deit-tf' , 'hf-internal-testing/tiny-random-roberta' ) lowerCAmelCase__ : Tuple = 13 lowerCAmelCase__ : Any = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : Dict = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Any = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : Tuple = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : str , a : Optional[Any] , a : Dict , a : Dict , a : Any , a : Any=None , **a : int ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Optional[int] = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : Any = model( input_ids=a , pixel_values=a , attention_mask=a , output_attentions=a ) lowerCAmelCase__ : Union[str, Any] = output.vision_model_output.attentions self.assertEqual(len(a ) , vision_config.num_hidden_layers ) # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) lowerCAmelCase__ : str = to_atuple(vision_model.config.image_size ) lowerCAmelCase__ : Union[str, Any] = to_atuple(vision_model.config.patch_size ) lowerCAmelCase__ : int = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase__ : int = num_patches + 2 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCAmelCase__ : List[str] = output.text_model_output.attentions self.assertEqual(len(a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _lowerCamelCase ( self : int , a : Optional[int] , a : int ): '''simple docstring''' lowerCAmelCase__ : Dict = TFDeiTModel(a , name='vision_model' ) lowerCAmelCase__ : List[Any] = TFRobertaModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = TFDeiTModelTester(self ) lowerCAmelCase__ : List[str] = TFRobertaModelTester(self ) lowerCAmelCase__ : str = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : List[Any] = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : List[str] = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : Any = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : int = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'Rocketknight1/tiny-random-clip-tf' , 'hf-internal-testing/tiny-random-bert' ) lowerCAmelCase__ : Dict = 13 lowerCAmelCase__ : str = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : List[Any] = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Union[str, Any] = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : Optional[int] = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : str , a : int , a : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = TFCLIPVisionModel(a , name='vision_model' ) lowerCAmelCase__ : List[str] = TFBertModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Any = TFCLIPVisionModelTester(self ) lowerCAmelCase__ : Union[str, Any] = TFBertModelTester(self ) lowerCAmelCase__ : Any = clip_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Any = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ : List[Any] = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : str = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_vision @require_tf class A__ ( unittest.TestCase ): @slow def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel.from_pretrained( 'clip-italian/clip-italian' , logit_scale_init_value=1.0 , from_pt=a ) lowerCAmelCase__ : List[Any] = VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian' ) lowerCAmelCase__ : int = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) lowerCAmelCase__ : Any = processor( text=['una foto di un gatto', 'una foto di un cane'] , images=a , padding=a , return_tensors='np' ) lowerCAmelCase__ : Union[str, Any] = model(**a ) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) ) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) lowerCAmelCase__ : List[str] = np.array([[1.2_2_8_4_7_2_7, 0.3_1_0_4_1_2_2]] ) self.assertTrue(np.allclose(outputs.logits_per_image.numpy() , a , atol=1E-3 ) )
307
0
import argparse import requests import torch from PIL import Image from torchvision.transforms import Compose, Normalize, Resize, ToTensor from transformers import SwinaSRConfig, SwinaSRForImageSuperResolution, SwinaSRImageProcessor def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: lowerCAmelCase__ : List[Any] = SwinaSRConfig() if "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: lowerCAmelCase__ : Optional[int] = 4 elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: lowerCAmelCase__ : Any = 4 lowerCAmelCase__ : Tuple = 48 lowerCAmelCase__ : Any = """pixelshuffle_aux""" elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: lowerCAmelCase__ : Any = [6, 6, 6, 6] lowerCAmelCase__ : Tuple = 60 lowerCAmelCase__ : int = [6, 6, 6, 6] lowerCAmelCase__ : List[Any] = """pixelshuffledirect""" elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: lowerCAmelCase__ : Optional[Any] = 4 lowerCAmelCase__ : Optional[Any] = """nearest+conv""" elif "Swin2SR_Jpeg_dynamic" in checkpoint_url: lowerCAmelCase__ : Tuple = 1 lowerCAmelCase__ : Optional[int] = 1 lowerCAmelCase__ : List[Any] = 126 lowerCAmelCase__ : Optional[Any] = 7 lowerCAmelCase__ : List[Any] = 255.0 lowerCAmelCase__ : Any = """""" return config def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: if "patch_embed.proj" in name and "layers" not in name: lowerCAmelCase__ : Tuple = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: lowerCAmelCase__ : List[str] = name.replace('patch_embed.norm' , 'embeddings.patch_embeddings.layernorm' ) if "layers" in name: lowerCAmelCase__ : Tuple = name.replace('layers' , 'encoder.stages' ) if "residual_group.blocks" in name: lowerCAmelCase__ : Tuple = name.replace('residual_group.blocks' , 'layers' ) if "attn.proj" in name: lowerCAmelCase__ : List[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__ : Dict = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: lowerCAmelCase__ : Union[str, Any] = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: lowerCAmelCase__ : List[Any] = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: lowerCAmelCase__ : List[str] = name.replace('mlp.fc2' , 'output.dense' ) if "q_bias" in name: lowerCAmelCase__ : List[str] = name.replace('q_bias' , 'query.bias' ) if "k_bias" in name: lowerCAmelCase__ : Tuple = name.replace('k_bias' , 'key.bias' ) if "v_bias" in name: lowerCAmelCase__ : Tuple = name.replace('v_bias' , 'value.bias' ) if "cpb_mlp" in name: lowerCAmelCase__ : Optional[Any] = name.replace('cpb_mlp' , 'continuous_position_bias_mlp' ) if "patch_embed.proj" in name: lowerCAmelCase__ : Any = name.replace('patch_embed.proj' , 'patch_embed.projection' ) if name == "norm.weight": lowerCAmelCase__ : Optional[Any] = """layernorm.weight""" if name == "norm.bias": lowerCAmelCase__ : str = """layernorm.bias""" if "conv_first" in name: lowerCAmelCase__ : str = name.replace('conv_first' , 'first_convolution' ) if ( "upsample" in name or "conv_before_upsample" in name or "conv_bicubic" in name or "conv_up" in name or "conv_hr" in name or "conv_last" in name or "aux" in name ): # heads if "conv_last" in name: lowerCAmelCase__ : Optional[int] = name.replace('conv_last' , 'final_convolution' ) if config.upsampler in ["pixelshuffle", "pixelshuffle_aux", "nearest+conv"]: if "conv_before_upsample.0" in name: lowerCAmelCase__ : int = name.replace('conv_before_upsample.0' , 'conv_before_upsample' ) if "upsample.0" in name: lowerCAmelCase__ : Dict = name.replace('upsample.0' , 'upsample.convolution_0' ) if "upsample.2" in name: lowerCAmelCase__ : Any = name.replace('upsample.2' , 'upsample.convolution_1' ) lowerCAmelCase__ : Optional[Any] = """upsample.""" + name elif config.upsampler == "pixelshuffledirect": lowerCAmelCase__ : Optional[Any] = name.replace('upsample.0.weight' , 'upsample.conv.weight' ) lowerCAmelCase__ : Any = name.replace('upsample.0.bias' , 'upsample.conv.bias' ) else: pass else: lowerCAmelCase__ : Dict = """swin2sr.""" + name return name def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: for key in orig_state_dict.copy().keys(): lowerCAmelCase__ : Tuple = orig_state_dict.pop(lowercase__ ) if "qkv" in key: lowerCAmelCase__ : Tuple = key.split('.' ) lowerCAmelCase__ : str = int(key_split[1] ) lowerCAmelCase__ : Tuple = int(key_split[4] ) lowerCAmelCase__ : str = config.embed_dim if "weight" in key: lowerCAmelCase__ : Union[str, Any] = val[:dim, :] lowerCAmelCase__ : List[Any] = val[dim : dim * 2, :] lowerCAmelCase__ : int = val[-dim:, :] else: lowerCAmelCase__ : List[Any] = val[:dim] lowerCAmelCase__ : Optional[int] = val[dim : dim * 2] lowerCAmelCase__ : List[Any] = val[-dim:] pass else: lowerCAmelCase__ : Tuple = val return orig_state_dict def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : List[str] = get_config(lowercase__ ) lowerCAmelCase__ : Any = SwinaSRForImageSuperResolution(lowercase__ ) model.eval() lowerCAmelCase__ : List[str] = torch.hub.load_state_dict_from_url(lowercase__ , map_location='cpu' ) lowerCAmelCase__ : List[Any] = convert_state_dict(lowercase__ , lowercase__ ) lowerCAmelCase__ : str = model.load_state_dict(lowercase__ , strict=lowercase__ ) if len(lowercase__ ) > 0: raise ValueError('Missing keys when converting: {}'.format(lowercase__ ) ) for key in unexpected_keys: if not ("relative_position_index" in key or "relative_coords_table" in key or "self_mask" in key): raise ValueError(F'''Unexpected key {key} in state_dict''' ) # verify values lowerCAmelCase__ : Tuple = """https://github.com/mv-lab/swin2sr/blob/main/testsets/real-inputs/shanghai.jpg?raw=true""" lowerCAmelCase__ : Optional[int] = Image.open(requests.get(lowercase__ , stream=lowercase__ ).raw ).convert('RGB' ) lowerCAmelCase__ : Dict = SwinaSRImageProcessor() # pixel_values = processor(image, return_tensors="pt").pixel_values lowerCAmelCase__ : Any = 126 if """Jpeg""" in checkpoint_url else 256 lowerCAmelCase__ : Optional[Any] = Compose( [ Resize((image_size, image_size) ), ToTensor(), Normalize(mean=[0.485, 0.456, 0.406] , std=[0.229, 0.224, 0.225] ), ] ) lowerCAmelCase__ : int = transforms(lowercase__ ).unsqueeze(0 ) if config.num_channels == 1: lowerCAmelCase__ : int = pixel_values[:, 0, :, :].unsqueeze(1 ) lowerCAmelCase__ : int = model(lowercase__ ) # assert values if "Swin2SR_ClassicalSR_X2_64" in checkpoint_url: lowerCAmelCase__ : Optional[int] = torch.Size([1, 3, 512, 512] ) lowerCAmelCase__ : Optional[Any] = torch.tensor( [[-0.7087, -0.7138, -0.6721], [-0.8340, -0.8095, -0.7298], [-0.9149, -0.8414, -0.7940]] ) elif "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: lowerCAmelCase__ : Optional[Any] = torch.Size([1, 3, 1_024, 1_024] ) lowerCAmelCase__ : List[Any] = torch.tensor( [[-0.7775, -0.8105, -0.8933], [-0.7764, -0.8356, -0.9225], [-0.7976, -0.8686, -0.9579]] ) elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: # TODO values didn't match exactly here lowerCAmelCase__ : List[str] = torch.Size([1, 3, 1_024, 1_024] ) lowerCAmelCase__ : str = torch.tensor( [[-0.8035, -0.7504, -0.7491], [-0.8538, -0.8124, -0.7782], [-0.8804, -0.8651, -0.8493]] ) elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: lowerCAmelCase__ : Tuple = torch.Size([1, 3, 512, 512] ) lowerCAmelCase__ : List[Any] = torch.tensor( [[-0.7669, -0.8662, -0.8767], [-0.8810, -0.9962, -0.9820], [-0.9340, -1.0322, -1.1149]] ) elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: lowerCAmelCase__ : List[str] = torch.Size([1, 3, 1_024, 1_024] ) lowerCAmelCase__ : Union[str, Any] = torch.tensor( [[-0.5238, -0.5557, -0.6321], [-0.6016, -0.5903, -0.6391], [-0.6244, -0.6334, -0.6889]] ) assert ( outputs.reconstruction.shape == expected_shape ), F'''Shape of reconstruction should be {expected_shape}, but is {outputs.reconstruction.shape}''' assert torch.allclose(outputs.reconstruction[0, 0, :3, :3] , lowercase__ , atol=1e-3 ) print('Looks ok!' ) lowerCAmelCase__ : Tuple = { """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth""": ( """swin2SR-classical-sr-x2-64""" ), """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X4_64.pth""": ( """swin2SR-classical-sr-x4-64""" ), """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_CompressedSR_X4_48.pth""": ( """swin2SR-compressed-sr-x4-48""" ), """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_Lightweight_X2_64.pth""": ( """swin2SR-lightweight-x2-64""" ), """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR.pth""": ( """swin2SR-realworld-sr-x4-64-bsrgan-psnr""" ), } lowerCAmelCase__ : Dict = url_to_name[checkpoint_url] if pytorch_dump_folder_path is not None: print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(lowercase__ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) processor.save_pretrained(lowercase__ ) if push_to_hub: model.push_to_hub(F'''caidas/{model_name}''' ) processor.push_to_hub(F'''caidas/{model_name}''' ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint_url""", default="""https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth""", type=str, help="""URL of the original Swin2SR 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.""" ) parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Whether to push the converted model to the hub.""") lowerCamelCase__ = parser.parse_args() convert_swinasr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
356
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) lowerCamelCase__ = logging.getLogger(__name__) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : Dict = np.argmax(SCREAMING_SNAKE_CASE_ , axis=1 ) return np.sum(outputs == labels ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: with open(SCREAMING_SNAKE_CASE_ , encoding='utf_8' ) as f: lowerCAmelCase__ : Dict = csv.reader(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = [] next(SCREAMING_SNAKE_CASE_ ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE_ ): output.append((' '.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: lowerCAmelCase__ : Dict = [] for dataset in encoded_datasets: lowerCAmelCase__ : List[str] = len(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase__ : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase__ : List[Any] = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) lowerCAmelCase__ : Tuple = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase__ : Any = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase__ : Optional[Any] = with_conta lowerCAmelCase__ : List[str] = with_conta lowerCAmelCase__ : List[Any] = len(SCREAMING_SNAKE_CASE_ ) - 1 lowerCAmelCase__ : Tuple = len(SCREAMING_SNAKE_CASE_ ) - 1 lowerCAmelCase__ : Tuple = with_conta lowerCAmelCase__ : Optional[int] = with_conta lowerCAmelCase__ : Optional[int] = mc_label lowerCAmelCase__ : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE_ ) for t in all_inputs ) ) return tensor_datasets def lowerCAmelCase__ ( ) -> int: lowerCAmelCase__ : int = argparse.ArgumentParser() parser.add_argument('--model_name' , type=SCREAMING_SNAKE_CASE_ , default='openai-gpt' , help='pretrained model name' ) parser.add_argument('--do_train' , action='store_true' , help='Whether to run training.' ) parser.add_argument('--do_eval' , action='store_true' , help='Whether to run eval on the dev set.' ) parser.add_argument( '--output_dir' , default=SCREAMING_SNAKE_CASE_ , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , help='The output directory where the model predictions and checkpoints will be written.' , ) parser.add_argument('--train_dataset' , type=SCREAMING_SNAKE_CASE_ , default='' ) parser.add_argument('--eval_dataset' , type=SCREAMING_SNAKE_CASE_ , default='' ) parser.add_argument('--seed' , type=SCREAMING_SNAKE_CASE_ , default=42 ) parser.add_argument('--num_train_epochs' , type=SCREAMING_SNAKE_CASE_ , default=3 ) parser.add_argument('--train_batch_size' , type=SCREAMING_SNAKE_CASE_ , default=8 ) parser.add_argument('--eval_batch_size' , type=SCREAMING_SNAKE_CASE_ , default=16 ) parser.add_argument('--adam_epsilon' , default=1e-8 , type=SCREAMING_SNAKE_CASE_ , help='Epsilon for Adam optimizer.' ) parser.add_argument('--max_grad_norm' , type=SCREAMING_SNAKE_CASE_ , default=1 ) parser.add_argument( '--max_steps' , default=-1 , type=SCREAMING_SNAKE_CASE_ , help=( 'If > 0: set total number of training steps to perform. Override num_train_epochs.' ) , ) parser.add_argument( '--gradient_accumulation_steps' , type=SCREAMING_SNAKE_CASE_ , default=1 , help='Number of updates steps to accumulate before performing a backward/update pass.' , ) parser.add_argument('--learning_rate' , type=SCREAMING_SNAKE_CASE_ , default=6.25e-5 ) parser.add_argument('--warmup_steps' , default=0 , type=SCREAMING_SNAKE_CASE_ , help='Linear warmup over warmup_steps.' ) parser.add_argument('--lr_schedule' , type=SCREAMING_SNAKE_CASE_ , default='warmup_linear' ) parser.add_argument('--weight_decay' , type=SCREAMING_SNAKE_CASE_ , default=0.01 ) parser.add_argument('--lm_coef' , type=SCREAMING_SNAKE_CASE_ , default=0.9 ) parser.add_argument('--n_valid' , type=SCREAMING_SNAKE_CASE_ , default=374 ) parser.add_argument('--server_ip' , type=SCREAMING_SNAKE_CASE_ , default='' , help='Can be used for distant debugging.' ) parser.add_argument('--server_port' , type=SCREAMING_SNAKE_CASE_ , default='' , help='Can be used for distant debugging.' ) lowerCAmelCase__ : List[str] = parser.parse_args() print(SCREAMING_SNAKE_CASE_ ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('Waiting for debugger attach' ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=SCREAMING_SNAKE_CASE_ ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) lowerCAmelCase__ : str = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) lowerCAmelCase__ : Dict = torch.cuda.device_count() logger.info('device: {}, n_gpu {}'.format(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) if not args.do_train and not args.do_eval: raise ValueError('At least one of `do_train` or `do_eval` must be True.' ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset lowerCAmelCase__ : Union[str, Any] = ['_start_', '_delimiter_', '_classify_'] lowerCAmelCase__ : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE_ ) ) model.to(SCREAMING_SNAKE_CASE_ ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE_ ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) ) elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE_ ) for o in obj] logger.info('Encoding dataset...' ) lowerCAmelCase__ : List[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase__ : str = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase__ : Union[str, Any] = (train_dataset, eval_dataset) lowerCAmelCase__ : List[str] = tokenize_and_encode(SCREAMING_SNAKE_CASE_ ) # Compute the max input length for the Transformer lowerCAmelCase__ : Union[str, Any] = model.config.n_positions // 2 - 2 lowerCAmelCase__ : Tuple = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) lowerCAmelCase__ : Dict = min(SCREAMING_SNAKE_CASE_ , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase__ : int = pre_process_datasets(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase__ : str = TensorDataset(*SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = RandomSampler(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = DataLoader(SCREAMING_SNAKE_CASE_ , sampler=SCREAMING_SNAKE_CASE_ , batch_size=args.train_batch_size ) lowerCAmelCase__ : Optional[Any] = TensorDataset(*SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = SequentialSampler(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = DataLoader(SCREAMING_SNAKE_CASE_ , sampler=SCREAMING_SNAKE_CASE_ , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase__ : Union[str, Any] = args.max_steps lowerCAmelCase__ : int = args.max_steps // (len(SCREAMING_SNAKE_CASE_ ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase__ : Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase__ : Optional[int] = list(model.named_parameters() ) lowerCAmelCase__ : Tuple = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] lowerCAmelCase__ : str = [ { 'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], 'weight_decay': args.weight_decay, }, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], 'weight_decay': 0.0}, ] lowerCAmelCase__ : Union[str, Any] = AdamW(SCREAMING_SNAKE_CASE_ , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase__ : int = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE_ , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE_ ) if args.do_train: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc='Epoch' ): lowerCAmelCase__ : str = 0 lowerCAmelCase__ : int = 0 lowerCAmelCase__ : str = tqdm(SCREAMING_SNAKE_CASE_ , desc='Training' ) for step, batch in enumerate(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Union[str, Any] = tuple(t.to(SCREAMING_SNAKE_CASE_ ) for t in batch ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Any = batch lowerCAmelCase__ : Tuple = model(SCREAMING_SNAKE_CASE_ , mc_token_ids=SCREAMING_SNAKE_CASE_ , lm_labels=SCREAMING_SNAKE_CASE_ , mc_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : str = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase__ : Optional[int] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase__ : List[str] = 'Training loss: {:.2e} lr: {:.2e}'.format(SCREAMING_SNAKE_CASE_ , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase__ : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE_ , 'module' ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase__ : List[str] = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : List[str] = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE_ ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE_ ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase__ : Dict = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase__ : List[Any] = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE_ ) if args.do_eval: model.eval() lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = 0, 0 lowerCAmelCase__ , lowerCAmelCase__ : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE_ , desc='Evaluating' ): lowerCAmelCase__ : str = tuple(t.to(SCREAMING_SNAKE_CASE_ ) for t in batch ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Tuple = batch with torch.no_grad(): lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : List[str] = model( SCREAMING_SNAKE_CASE_ , mc_token_ids=SCREAMING_SNAKE_CASE_ , lm_labels=SCREAMING_SNAKE_CASE_ , mc_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = mc_logits.detach().cpu().numpy() lowerCAmelCase__ : List[Any] = mc_labels.to('cpu' ).numpy() lowerCAmelCase__ : str = accuracy(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase__ : Optional[int] = eval_loss / nb_eval_steps lowerCAmelCase__ : Any = eval_accuracy / nb_eval_examples lowerCAmelCase__ : Union[str, Any] = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase__ : Tuple = {'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'train_loss': train_loss} lowerCAmelCase__ : Dict = os.path.join(args.output_dir , 'eval_results.txt' ) with open(SCREAMING_SNAKE_CASE_ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key in sorted(result.keys() ): logger.info(' %s = %s' , SCREAMING_SNAKE_CASE_ , str(result[key] ) ) writer.write('%s = %s\n' % (key, str(result[key] )) ) if __name__ == "__main__": main()
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: lowerCAmelCase__ : Dict = word.split() def justify(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : Dict = max_width - width lowerCAmelCase__ : Dict = len(A__ ) if len(A__ ) == 1: # if there is only word in line # just insert overall_spaces_count for the remainder of line return line[0] + " " * overall_spaces_count else: lowerCAmelCase__ : Optional[int] = words_count - 1 # num_spaces_between_words_list[i] : tells you to insert # num_spaces_between_words_list[i] spaces # after word on line[i] lowerCAmelCase__ : List[str] = spaces_to_insert_between_words * [ overall_spaces_count // spaces_to_insert_between_words ] lowerCAmelCase__ : Union[str, Any] = ( overall_spaces_count % spaces_to_insert_between_words ) # distribute spaces via round robin to the left words for i in range(A__ ): num_spaces_between_words_list[i] += 1 lowerCAmelCase__ : List[Any] = [] for i in range(A__ ): # add the word aligned_words_list.append(line[i] ) # add the spaces to insert aligned_words_list.append(num_spaces_between_words_list[i] * ' ' ) # just add the last word to the sentence aligned_words_list.append(line[-1] ) # join the aligned words list to form a justified line return "".join(A__ ) lowerCAmelCase__ : int = [] lowerCAmelCase__ : Any = [] lowerCAmelCase__ : List[str] = 0 for word in words: if width + len(A__ ) + len(A__ ) <= max_width: # keep adding words until we can fill out max_width # width = sum of length of all words (without overall_spaces_count) # len(word) = length of current word # len(line) = number of overall_spaces_count to insert between words line.append(A__ ) width += len(A__ ) else: # justify the line and add it to result answer.append(justify(A__ , A__ , A__ ) ) # reset new line and new width lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = [word], len(A__ ) lowerCAmelCase__ : Tuple = max_width - width - len(A__ ) answer.append(' '.join(A__ ) + (remaining_spaces + 1) * ' ' ) return answer if __name__ == "__main__": from doctest import testmod testmod()
357
import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers lowerCamelCase__ = """python tqdm regex requests packaging filelock numpy tokenizers""".split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append("""dataclasses""") if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append("""importlib_metadata""") for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F"""can't find {pkg} in {deps.keys()}, check dependency_versions_table.py""") def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> int: require_version(deps[pkg] , SCREAMING_SNAKE_CASE_ )
307
0
import os # Precomputes a list of the 100 first triangular numbers lowerCamelCase__ = [int(0.5 * n * (n + 1)) for n in range(1, 101)] def lowerCAmelCase__ ( ) -> List[Any]: lowerCAmelCase__ : List[Any] = os.path.dirname(os.path.realpath(SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase__ : List[str] = os.path.join(SCREAMING_SNAKE_CASE_ , 'words.txt' ) lowerCAmelCase__ : Optional[int] = '' with open(SCREAMING_SNAKE_CASE_ ) as f: lowerCAmelCase__ : List[Any] = f.readline() lowerCAmelCase__ : str = [word.strip('\"' ) for word in words.strip('\r\n' ).split(',' )] lowerCAmelCase__ : Union[str, Any] = [ word for word in [sum(ord(SCREAMING_SNAKE_CASE_ ) - 64 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": print(solution())
358
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class A__ ( __magic_name__ ): lowercase = (DPMSolverSDEScheduler,) lowercase = 10 def _lowerCamelCase ( self : Optional[int] , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = { 'num_train_timesteps': 1_100, 'beta_start': 0.0_0_0_1, 'beta_end': 0.0_2, 'beta_schedule': 'linear', 'noise_sampler_seed': 0, } config.update(**a ) return config def _lowerCamelCase ( self : Tuple ): '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=a ) def _lowerCamelCase ( self : int ): '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=a , beta_end=a ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=a ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = self.scheduler_classes[0] lowerCAmelCase__ : Tuple = self.get_scheduler_config() lowerCAmelCase__ : List[Any] = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Dict = self.dummy_model() lowerCAmelCase__ : int = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : int = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : List[Any] = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : int = scheduler.step(a , a , a ) lowerCAmelCase__ : Any = output.prev_sample lowerCAmelCase__ : List[Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Optional[int] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_7_8_2_1_0_4_4_9_2_1_8_7_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_7_8_7_0_5_9_6_4_5_6_5_2_7_7 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_2_1_1_1_8_1_6_4_0_6 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_6_8_9_2_2_9_9_6_5_2 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.scheduler_classes[0] lowerCAmelCase__ : List[str] = self.get_scheduler_config(prediction_type='v_prediction' ) lowerCAmelCase__ : Any = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Optional[int] = self.dummy_model() lowerCAmelCase__ : Union[str, Any] = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : Any = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : str = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : Dict = scheduler.step(a , a , a ) lowerCAmelCase__ : Tuple = output.prev_sample lowerCAmelCase__ : int = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Union[str, Any] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_2_4.7_7_1_4_9_2_0_0_4_3_9_4_5_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_2_2_6_2_8_9_0_1_4_8_1_6_2_8_4 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_2_8.1_6_6_3_3_6_0_5_9_5_7_0_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_6_8_8_3_2_6_0_0_1_1_6_7_2_9_7 ) < 1E-3 else: assert abs(result_sum.item() - 1_1_9.8_4_8_7_5_4_8_8_2_8_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.1_5_6_0_5_3_0_6_6_2_5_3_6_6_2_1 ) < 1E-3 def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.scheduler_classes[0] lowerCAmelCase__ : Optional[int] = self.get_scheduler_config() lowerCAmelCase__ : int = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : Tuple = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: lowerCAmelCase__ : Dict = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : Optional[int] = model(a , a ) lowerCAmelCase__ : Tuple = scheduler.step(a , a , a ) lowerCAmelCase__ : Dict = output.prev_sample lowerCAmelCase__ : Union[str, Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Dict = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_6_9_5_7_3_9_7_4_6_0_9_3_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_8_0_5_9_3_4_6_0_7_9_8_2_6_3_5 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_3_6_3_7_6_9_5_3_1_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_8_3_8_2_4_1_5_7_7_1 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.scheduler_classes[0] lowerCAmelCase__ : Any = self.get_scheduler_config() lowerCAmelCase__ : Any = scheduler_class(**a , use_karras_sigmas=a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : str = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma lowerCAmelCase__ : str = sample.to(a ) for t in scheduler.timesteps: lowerCAmelCase__ : Any = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : int = model(a , a ) lowerCAmelCase__ : Union[str, Any] = scheduler.step(a , a , a ) lowerCAmelCase__ : Union[str, Any] = output.prev_sample lowerCAmelCase__ : Optional[int] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Any = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_7_6.6_6_9_7_4_1_3_5_7_4_2_1_8_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_7.6_3_6_5_3_5_6_4_4_5_3_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 else: assert abs(result_sum.item() - 1_7_0.3_1_3_5_2_2_3_3_8_8_6_7_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2
307
0
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 lowerCamelCase__ = logging.get_logger(__name__) class A__ ( SCREAMING_SNAKE_CASE__ ): def __init__( self : Optional[int] , **a : Tuple ): '''simple docstring''' requires_backends(self , ['bs4'] ) super().__init__(**a_ ) def _lowerCamelCase ( self : List[Any] , a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = [] lowerCAmelCase__ : List[Any] = [] lowerCAmelCase__ : Any = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag lowerCAmelCase__ : int = parent.find_all(child.name , recursive=a_ ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(a_ ) else next(i for i, s in enumerate(a_ , 1 ) if s is child ) ) lowerCAmelCase__ : Optional[Any] = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def _lowerCamelCase ( self : Dict , a : int ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = BeautifulSoup(a_ , 'html.parser' ) lowerCAmelCase__ : List[Any] = [] lowerCAmelCase__ : Any = [] lowerCAmelCase__ : List[str] = [] for element in html_code.descendants: if type(a_ ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue lowerCAmelCase__ : Any = html.unescape(a_ ).strip() if not text_in_this_tag: continue all_doc_strings.append(a_ ) lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = self.xpath_soup(a_ ) stringaxtag_seq.append(a_ ) stringaxsubs_seq.append(a_ ) if len(a_ ) != len(a_ ): raise ValueError('Number of doc strings and xtags does not correspond' ) if len(a_ ) != len(a_ ): raise ValueError('Number of doc strings and xsubs does not correspond' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def _lowerCamelCase ( self : List[Any] , a : str , a : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = '' for tagname, subs in zip(a_ , a_ ): xpath += f'''/{tagname}''' if subs != 0: xpath += f'''[{subs}]''' return xpath def __call__( self : Tuple , a : List[str] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = False # Check that strings has a valid type if isinstance(a_ , a_ ): lowerCAmelCase__ : str = True elif isinstance(a_ , (list, tuple) ): if len(a_ ) == 0 or isinstance(html_strings[0] , a_ ): lowerCAmelCase__ : Any = True if not valid_strings: raise ValueError( 'HTML strings must of type `str`, `List[str]` (batch of examples), ' f'''but is of type {type(a_ )}.''' ) lowerCAmelCase__ : Optional[int] = bool(isinstance(a_ , (list, tuple) ) and (isinstance(html_strings[0] , a_ )) ) if not is_batched: lowerCAmelCase__ : Tuple = [html_strings] # Get nodes + xpaths lowerCAmelCase__ : List[Any] = [] lowerCAmelCase__ : Any = [] for html_string in html_strings: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Any = self.get_three_from_single(a_ ) nodes.append(a_ ) lowerCAmelCase__ : int = [] for node, tag_list, sub_list in zip(a_ , a_ , a_ ): lowerCAmelCase__ : Tuple = self.construct_xpath(a_ , a_ ) xpath_strings.append(a_ ) xpaths.append(a_ ) # return as Dict lowerCAmelCase__ : Optional[Any] = {'nodes': nodes, 'xpaths': xpaths} lowerCAmelCase__ : Optional[Any] = BatchFeature(data=a_ , tensor_type=a_ ) return encoded_inputs
359
import os import string import sys lowerCamelCase__ = 1 << 8 lowerCamelCase__ = { """tab""": ord("""\t"""), """newline""": ord("""\r"""), """esc""": 27, """up""": 65 + ARROW_KEY_FLAG, """down""": 66 + ARROW_KEY_FLAG, """right""": 67 + ARROW_KEY_FLAG, """left""": 68 + ARROW_KEY_FLAG, """mod_int""": 91, """undefined""": sys.maxsize, """interrupt""": 3, """insert""": 50, """delete""": 51, """pg_up""": 53, """pg_down""": 54, } lowerCamelCase__ = KEYMAP["""up"""] lowerCamelCase__ = KEYMAP["""left"""] if sys.platform == "win32": lowerCamelCase__ = [] lowerCamelCase__ = { b"""\xe0H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\x00H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\xe0P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\x00P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\xe0M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\x00M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\xe0K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, b"""\x00K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, } for i in range(10): lowerCamelCase__ = ord(str(i)) def lowerCAmelCase__ ( ) -> Dict: if os.name == "nt": import msvcrt lowerCAmelCase__ : Dict = 'mbcs' # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(SCREAMING_SNAKE_CASE_ ) == 0: # Read the keystroke lowerCAmelCase__ : Optional[Any] = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): lowerCAmelCase__ : Dict = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: lowerCAmelCase__ : Dict = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP['mod_int'] ) ) WIN_CH_BUFFER.append(SCREAMING_SNAKE_CASE_ ) if ord(SCREAMING_SNAKE_CASE_ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) lowerCAmelCase__ : Dict = chr(KEYMAP['esc'] ) except KeyError: lowerCAmelCase__ : Dict = cha[1] else: lowerCAmelCase__ : List[Any] = ch.decode(SCREAMING_SNAKE_CASE_ ) else: lowerCAmelCase__ : Tuple = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty lowerCAmelCase__ : Tuple = sys.stdin.fileno() lowerCAmelCase__ : Any = termios.tcgetattr(SCREAMING_SNAKE_CASE_ ) try: tty.setraw(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = sys.stdin.read(1 ) finally: termios.tcsetattr(SCREAMING_SNAKE_CASE_ , termios.TCSADRAIN , SCREAMING_SNAKE_CASE_ ) return ch def lowerCAmelCase__ ( ) -> Union[str, Any]: lowerCAmelCase__ : Any = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["esc"]: lowerCAmelCase__ : Union[str, Any] = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["mod_int"]: lowerCAmelCase__ : str = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(SCREAMING_SNAKE_CASE_ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(SCREAMING_SNAKE_CASE_ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
307
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ = logging.get_logger(__name__) class A__ ( __magic_name__ ): def __init__( self : str , *a : Dict , **a : int ): '''simple docstring''' warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , __lowerCAmelCase , ) super().__init__(*__lowerCAmelCase , **__lowerCAmelCase )
360
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: return "".join([hex(SCREAMING_SNAKE_CASE_ )[2:].zfill(2 ).upper() for byte in list(SCREAMING_SNAKE_CASE_ )] ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> bytes: # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(SCREAMING_SNAKE_CASE_ ) % 2) != 0: raise ValueError( 'Base16 encoded data is invalid:\nData does not have an even number of hex digits.' ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(SCREAMING_SNAKE_CASE_ ) <= set('0123456789ABCDEF' ): raise ValueError( 'Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.' ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
307
0
# DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim from dataclasses import dataclass from typing import Optional, Tuple, Union import flax import jax import jax.numpy as jnp from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils_flax import ( CommonSchedulerState, FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, add_noise_common, get_velocity_common, ) @flax.struct.dataclass class A__ : lowercase = 42 # setable values lowercase = 42 lowercase = 42 lowercase = None @classmethod def _lowerCamelCase ( cls : Any , a : Union[str, Any] , a : Optional[int] , a : List[Any] ): '''simple docstring''' return cls(common=_snake_case , init_noise_sigma=_snake_case , timesteps=_snake_case ) @dataclass class A__ ( __magic_name__ ): lowercase = 42 class A__ ( __magic_name__ , __magic_name__ ): lowercase = [e.name for e in FlaxKarrasDiffusionSchedulers] lowercase = 42 @property def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' return True @register_to_config def __init__( self : Union[str, Any] , a : int = 1_000 , a : int = 0.0_0_0_1 , a : Union[str, Any] = 0.0_2 , a : List[str] = "linear" , a : Dict = None , a : Dict = "fixed_small" , a : int = True , a : Union[str, Any] = "epsilon" , a : Tuple = jnp.floataa , ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = dtype def _lowerCamelCase ( self : Optional[Any] , a : Optional[int] = None ): '''simple docstring''' if common is None: lowerCAmelCase__ : Tuple = CommonSchedulerState.create(self ) # standard deviation of the initial noise distribution lowerCAmelCase__ : str = jnp.array(1.0 , dtype=self.dtype ) lowerCAmelCase__ : Dict = jnp.arange(0 , self.config.num_train_timesteps ).round()[::-1] return DDPMSchedulerState.create( common=_snake_case , init_noise_sigma=_snake_case , timesteps=_snake_case , ) def _lowerCamelCase ( self : Optional[int] , a : Dict , a : List[str] , a : Union[str, Any] = None ): '''simple docstring''' return sample def _lowerCamelCase ( self : str , a : Optional[Any] , a : Optional[Any] , a : Any = () ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.config.num_train_timesteps // num_inference_steps # creates integer timesteps by multiplying by ratio # rounding to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : Any = (jnp.arange(0 , _snake_case ) * step_ratio).round()[::-1] return state.replace( num_inference_steps=_snake_case , timesteps=_snake_case , ) def _lowerCamelCase ( self : int , a : int , a : str , a : Optional[Any]=None , a : List[Any]=None ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = state.common.alphas_cumprod[t] lowerCAmelCase__ : str = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample lowerCAmelCase__ : Tuple = (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * state.common.betas[t] if variance_type is None: lowerCAmelCase__ : Optional[int] = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small": lowerCAmelCase__ : Tuple = jnp.clip(_snake_case , a_min=1E-20 ) # for rl-diffuser https://arxiv.org/abs/2205.09991 elif variance_type == "fixed_small_log": lowerCAmelCase__ : Dict = jnp.log(jnp.clip(_snake_case , a_min=1E-20 ) ) elif variance_type == "fixed_large": lowerCAmelCase__ : List[str] = state.common.betas[t] elif variance_type == "fixed_large_log": # Glide max_log lowerCAmelCase__ : str = jnp.log(state.common.betas[t] ) elif variance_type == "learned": return predicted_variance elif variance_type == "learned_range": lowerCAmelCase__ : Optional[Any] = variance lowerCAmelCase__ : Union[str, Any] = state.common.betas[t] lowerCAmelCase__ : Optional[int] = (predicted_variance + 1) / 2 lowerCAmelCase__ : List[Any] = frac * max_log + (1 - frac) * min_log return variance def _lowerCamelCase ( self : Tuple , a : Optional[Any] , a : int , a : Union[str, Any] , a : Optional[Any] , a : int = None , a : int = True , ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = timestep if key is None: lowerCAmelCase__ : Optional[int] = jax.random.PRNGKey(0 ) if model_output.shape[1] == sample.shape[1] * 2 and self.config.variance_type in ["learned", "learned_range"]: lowerCAmelCase__ , lowerCAmelCase__ : Dict = jnp.split(_snake_case , sample.shape[1] , axis=1 ) else: lowerCAmelCase__ : Dict = None # 1. compute alphas, betas lowerCAmelCase__ : Union[str, Any] = state.common.alphas_cumprod[t] lowerCAmelCase__ : Dict = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) lowerCAmelCase__ : Dict = 1 - alpha_prod_t lowerCAmelCase__ : Union[str, Any] = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": lowerCAmelCase__ : str = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": lowerCAmelCase__ : Optional[Any] = model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase__ : Optional[int] = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output else: raise ValueError( f'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample` ''' ' for the FlaxDDPMScheduler.' ) # 3. Clip "predicted x_0" if self.config.clip_sample: lowerCAmelCase__ : Dict = jnp.clip(_snake_case , -1 , 1 ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase__ : Union[str, Any] = (alpha_prod_t_prev ** 0.5 * state.common.betas[t]) / beta_prod_t lowerCAmelCase__ : int = state.common.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase__ : Dict = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise def random_variance(): lowerCAmelCase__ : Optional[int] = jax.random.split(_snake_case , num=1 ) lowerCAmelCase__ : Optional[int] = jax.random.normal(_snake_case , shape=model_output.shape , dtype=self.dtype ) return (self._get_variance(_snake_case , _snake_case , predicted_variance=_snake_case ) ** 0.5) * noise lowerCAmelCase__ : Optional[Any] = jnp.where(t > 0 , random_variance() , jnp.zeros(model_output.shape , dtype=self.dtype ) ) lowerCAmelCase__ : Any = pred_prev_sample + variance if not return_dict: return (pred_prev_sample, state) return FlaxDDPMSchedulerOutput(prev_sample=_snake_case , state=_snake_case ) def _lowerCamelCase ( self : Optional[Any] , a : str , a : Union[str, Any] , a : Tuple , a : Optional[Any] , ): '''simple docstring''' return add_noise_common(state.common , _snake_case , _snake_case , _snake_case ) def _lowerCamelCase ( self : Optional[Any] , a : Dict , a : Optional[Any] , a : Union[str, Any] , a : int , ): '''simple docstring''' return get_velocity_common(state.common , _snake_case , _snake_case , _snake_case ) def __len__( self : Any ): '''simple docstring''' return self.config.num_train_timesteps
361
from __future__ import annotations def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[list[int]]: lowerCAmelCase__ : list[list[int]] = [] create_all_state(1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , [] , SCREAMING_SNAKE_CASE_ ) return result def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) -> None: if level == 0: total_list.append(current_list[:] ) return for i in range(SCREAMING_SNAKE_CASE_ , total_number - level + 2 ): current_list.append(SCREAMING_SNAKE_CASE_ ) create_all_state(i + 1 , SCREAMING_SNAKE_CASE_ , level - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) current_list.pop() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> None: for i in total_list: print(*SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCamelCase__ = 4 lowerCamelCase__ = 2 lowerCamelCase__ = generate_all_combinations(n, k) print_all_state(total_list)
307
0
class A__ : def __init__( self : Any ): '''simple docstring''' lowerCAmelCase__ : int = {} def _lowerCamelCase ( self : Tuple ): '''simple docstring''' print(self.vertex ) for i in self.vertex: print(a , ' -> ' , ' -> '.join([str(a ) for j in self.vertex[i]] ) ) def _lowerCamelCase ( self : List[Any] , a : Union[str, Any] , a : Any ): '''simple docstring''' if from_vertex in self.vertex: self.vertex[from_vertex].append(a ) else: # else make a new vertex lowerCAmelCase__ : Union[str, Any] = [to_vertex] def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : int = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(a , a ) def _lowerCamelCase ( self : Tuple , a : List[str] , a : int ): '''simple docstring''' lowerCAmelCase__ : Any = True print(a , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(a , a ) if __name__ == "__main__": lowerCamelCase__ = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print("""DFS:""") g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
362
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class A__ ( unittest.TestCase ): @parameterized.expand([(None,), ('foo.json',)] ) def _lowerCamelCase ( self : Dict , a : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a , config_name=a ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(a , config_name=a ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 50 ) self.assertEqual(loaded_config.max_length , 20 ) self.assertEqual(loaded_config.max_time , a ) def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Dict = AutoConfig.from_pretrained('gpt2' ) lowerCAmelCase__ : Any = GenerationConfig.from_model_config(a ) lowerCAmelCase__ : Any = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(a , a ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : Dict = { 'max_new_tokens': 1_024, 'foo': 'bar', } lowerCAmelCase__ : List[Any] = copy.deepcopy(a ) lowerCAmelCase__ : Dict = generation_config.update(**a ) # update_kwargs was not modified (no side effects) self.assertEqual(a , a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_024 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(a , {'foo': 'bar'} ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : List[Any] = 'bar' with tempfile.TemporaryDirectory('test-generation-config' ) as tmp_dir: generation_config.save_pretrained(a ) lowerCAmelCase__ : List[Any] = GenerationConfig.from_pretrained(a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , 'bar' ) lowerCAmelCase__ : int = GenerationConfig.from_model_config(a ) assert not hasattr(a , 'foo' ) # no new kwargs should be initialized if from config def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , a ) self.assertEqual(default_config.num_beams , 1 ) lowerCAmelCase__ : List[Any] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , a ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(a , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class A__ ( unittest.TestCase ): @classmethod def _lowerCamelCase ( cls : int ): '''simple docstring''' lowerCAmelCase__ : List[str] = TOKEN HfFolder.save_token(a ) @classmethod def _lowerCamelCase ( cls : Optional[int] ): '''simple docstring''' try: delete_repo(token=cls._token , repo_id='test-generation-config' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-generation-config-org' ) except HTTPError: pass def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('test-generation-config' , use_auth_token=self._token ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='test-generation-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='test-generation-config' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('valid_org/test-generation-config-org' , use_auth_token=self._token ) lowerCAmelCase__ : Dict = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-generation-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='valid_org/test-generation-config-org' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : List[str] = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) )
307
0
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets lowerCamelCase__ = """\ @inproceedings{snover-etal-2006-study, title = \"A Study of Translation Edit Rate with Targeted Human Annotation\", author = \"Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John\", booktitle = \"Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers\", month = aug # \" 8-12\", year = \"2006\", address = \"Cambridge, Massachusetts, USA\", publisher = \"Association for Machine Translation in the Americas\", url = \"https://aclanthology.org/2006.amta-papers.25\", pages = \"223--231\", } @inproceedings{post-2018-call, title = \"A Call for Clarity in Reporting {BLEU} Scores\", author = \"Post, Matt\", booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\", month = oct, year = \"2018\", address = \"Belgium, Brussels\", publisher = \"Association for Computational Linguistics\", url = \"https://www.aclweb.org/anthology/W18-6319\", pages = \"186--191\", } """ lowerCamelCase__ = """\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu's required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. """ lowerCamelCase__ = """ Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: 'score' (float): TER score (num_edits / sum_ref_lengths * 100) 'num_edits' (int): The cumulative number of edits 'ref_length' (float): The cumulative average reference length Examples: Example 1: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\", ... \"What did the TER metric user say to the developer?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"], ... [\"Your jokes are...\", \"...TERrible\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {'score': 150.0, 'num_edits': 15, 'ref_length': 10.0} Example 2: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {'score': 62.5, 'num_edits': 5, 'ref_length': 8.0} Example 3: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {'score': 57.14285714285714, 'num_edits': 6, 'ref_length': 10.5} Example 4: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {'score': 0.0, 'num_edits': 0, 'ref_length': 8.0} Example 5: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\", ... \"What did the TER metric user say to the developer?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"], ... [\"Your jokes are...\", \"...TERrible\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {'score': 100.0, 'num_edits': 10, 'ref_length': 10.0} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A__ ( datasets.Metric ): def _lowerCamelCase ( self : str ): '''simple docstring''' if version.parse(scb.__version__ ) < version.parse('1.4.12' ): raise ImportWarning( 'To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n' 'You can install it with `pip install "sacrebleu>=1.4.12"`.' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='http://www.cs.umd.edu/~snover/tercom/' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Sequence(datasets.Value('string' , id='sequence' ) , id='references' ), } ) , codebase_urls=['https://github.com/mjpost/sacreBLEU#ter'] , reference_urls=[ 'https://github.com/jhclark/tercom', ] , ) def _lowerCamelCase ( self : Optional[int] , a : Any , a : List[Any] , a : bool = False , a : bool = False , a : bool = False , a : bool = False , ): '''simple docstring''' lowerCAmelCase__ : Tuple = 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' ) lowerCAmelCase__ : str = [[refs[i] for refs in references] for i in range(_a )] lowerCAmelCase__ : Tuple = TER( normalized=_a , no_punct=_a , asian_support=_a , case_sensitive=_a , ) lowerCAmelCase__ : Optional[Any] = sb_ter.corpus_score(_a , _a ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
363
import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import ( DiffusionPipeline, UnCLIPImageVariationPipeline, UnCLIPScheduler, UNetaDConditionModel, UNetaDModel, ) from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, load_image, require_torch_gpu, skip_mps from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = UnCLIPImageVariationPipeline lowercase = IMAGE_VARIATION_PARAMS - {'height', 'width', 'guidance_scale'} lowercase = IMAGE_VARIATION_BATCH_PARAMS lowercase = [ 'generator', 'return_dict', 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] lowercase = False @property def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : int ): '''simple docstring''' return self.time_input_dim @property def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return self.time_input_dim * 4 @property def _lowerCamelCase ( self : Any ): '''simple docstring''' return 100 @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) return tokenizer @property def _lowerCamelCase ( self : Any ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) return CLIPTextModelWithProjection(a ) @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : List[Any] = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) return CLIPVisionModelWithProjection(a ) @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Union[str, Any] = { 'clip_embeddings_dim': self.text_embedder_hidden_size, 'time_embed_dim': self.time_embed_dim, 'cross_attention_dim': self.cross_attention_dim, } lowerCAmelCase__ : Optional[Any] = UnCLIPTextProjModel(**a ) return model @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : str = { 'sample_size': 32, # RGB in channels 'in_channels': 3, # Out channels is double in channels because predicts mean and variance 'out_channels': 6, 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': 'identity', } lowerCAmelCase__ : str = UNetaDConditionModel(**a ) return model @property def _lowerCamelCase ( self : str ): '''simple docstring''' return { "sample_size": 64, "layers_per_block": 1, "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"), "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"), "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "in_channels": 6, "out_channels": 3, } @property def _lowerCamelCase ( self : str ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Any = UNetaDModel(**self.dummy_super_res_kwargs ) return model @property def _lowerCamelCase ( self : int ): '''simple docstring''' torch.manual_seed(1 ) lowerCAmelCase__ : List[str] = UNetaDModel(**self.dummy_super_res_kwargs ) return model def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.dummy_decoder lowerCAmelCase__ : Optional[int] = self.dummy_text_proj lowerCAmelCase__ : Any = self.dummy_text_encoder lowerCAmelCase__ : Any = self.dummy_tokenizer lowerCAmelCase__ : Any = self.dummy_super_res_first lowerCAmelCase__ : Optional[int] = self.dummy_super_res_last lowerCAmelCase__ : Dict = UnCLIPScheduler( variance_type='learned_range' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = UnCLIPScheduler( variance_type='fixed_small_log' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = CLIPImageProcessor(crop_size=32 , size=32 ) lowerCAmelCase__ : Optional[int] = self.dummy_image_encoder return { "decoder": decoder, "text_encoder": text_encoder, "tokenizer": tokenizer, "text_proj": text_proj, "feature_extractor": feature_extractor, "image_encoder": image_encoder, "super_res_first": super_res_first, "super_res_last": super_res_last, "decoder_scheduler": decoder_scheduler, "super_res_scheduler": super_res_scheduler, } def _lowerCamelCase ( self : Any , a : Dict , a : List[str]=0 , a : List[str]=True ): '''simple docstring''' lowerCAmelCase__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(a ) ).to(a ) if str(a ).startswith('mps' ): lowerCAmelCase__ : Optional[int] = torch.manual_seed(a ) else: lowerCAmelCase__ : str = torch.Generator(device=a ).manual_seed(a ) if pil_image: lowerCAmelCase__ : Optional[int] = input_image * 0.5 + 0.5 lowerCAmelCase__ : Dict = input_image.clamp(0 , 1 ) lowerCAmelCase__ : List[Any] = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() lowerCAmelCase__ : Union[str, Any] = DiffusionPipeline.numpy_to_pil(a )[0] return { "image": input_image, "generator": generator, "decoder_num_inference_steps": 2, "super_res_num_inference_steps": 2, "output_type": "np", } def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = 'cpu' lowerCAmelCase__ : Any = self.get_dummy_components() lowerCAmelCase__ : List[str] = self.pipeline_class(**a ) lowerCAmelCase__ : Dict = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : str = pipe(**a ) lowerCAmelCase__ : Optional[Any] = output.images lowerCAmelCase__ : str = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : List[str] = np.array( [ 0.9_9_9_7, 0.0_0_0_2, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_6_9, 0.0_0_2_3, 0.9_9_9_7, 0.9_9_6_9, 0.9_9_7_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 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = 'cpu' lowerCAmelCase__ : Dict = self.get_dummy_components() lowerCAmelCase__ : Optional[int] = self.pipeline_class(**a ) lowerCAmelCase__ : int = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = pipe(**a ) lowerCAmelCase__ : Union[str, Any] = output.images lowerCAmelCase__ : int = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : int = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Tuple = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : str = np.array([0.9_9_9_7, 0.0_0_0_3, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_7_0, 0.0_0_2_4, 0.9_9_9_7, 0.9_9_7_1, 0.9_9_7_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Tuple = 'cpu' lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : Tuple = self.pipeline_class(**a ) lowerCAmelCase__ : Union[str, Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Tuple = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = [ pipeline_inputs['image'], pipeline_inputs['image'], ] lowerCAmelCase__ : Optional[int] = pipe(**a ) lowerCAmelCase__ : Tuple = output.images lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Union[str, Any] = [ tuple_pipeline_inputs['image'], tuple_pipeline_inputs['image'], ] lowerCAmelCase__ : str = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Any = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (2, 64, 64, 3) lowerCAmelCase__ : Union[str, Any] = np.array( [ 0.9_9_9_7, 0.9_9_8_9, 0.0_0_0_8, 0.0_0_2_1, 0.9_9_6_0, 0.0_0_1_8, 0.0_0_1_4, 0.0_0_0_2, 0.9_9_3_3, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch.device('cpu' ) class A__ : lowercase = 1 lowerCAmelCase__ : Optional[Any] = self.get_dummy_components() lowerCAmelCase__ : Dict = self.pipeline_class(**a ) lowerCAmelCase__ : Optional[Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.Generator(device=a ).manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe.decoder.dtype lowerCAmelCase__ : Union[str, Any] = 1 lowerCAmelCase__ : str = ( batch_size, pipe.decoder.config.in_channels, pipe.decoder.config.sample_size, pipe.decoder.config.sample_size, ) lowerCAmelCase__ : List[Any] = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[str] = ( batch_size, pipe.super_res_first.config.in_channels // 2, pipe.super_res_first.config.sample_size, pipe.super_res_first.config.sample_size, ) lowerCAmelCase__ : Any = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , decoder_latents=a , super_res_latents=a ).images lowerCAmelCase__ : Optional[Any] = self.get_dummy_inputs(a , pil_image=a ) # Don't pass image, instead pass embedding lowerCAmelCase__ : Union[str, Any] = pipeline_inputs.pop('image' ) lowerCAmelCase__ : Union[str, Any] = pipe.image_encoder(a ).image_embeds lowerCAmelCase__ : List[Any] = pipe( **a , decoder_latents=a , super_res_latents=a , image_embeddings=a , ).images # make sure passing text embeddings manually is identical assert np.abs(img_out_a - img_out_a ).max() < 1E-4 @skip_mps def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device == 'cpu' # Check is relaxed because there is not a torch 2.0 sliced attention added kv processor lowerCAmelCase__ : int = 1E-2 self._test_attention_slicing_forward_pass( test_max_difference=a , expected_max_diff=a ) @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = torch_device == 'cpu' lowerCAmelCase__ : Any = True lowerCAmelCase__ : Optional[Any] = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] self._test_inference_batch_single_identical( test_max_difference=a , relax_max_difference=a , additional_params_copy_to_batched_inputs=a , ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] if torch_device == "mps": # TODO: MPS errors with larger batch sizes lowerCAmelCase__ : List[str] = [2, 3] self._test_inference_batch_consistent( batch_sizes=a , additional_params_copy_to_batched_inputs=a , ) else: self._test_inference_batch_consistent( additional_params_copy_to_batched_inputs=a ) @skip_mps def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent() @skip_mps def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_optional_components() @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Tuple ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/unclip/cat.png' ) lowerCAmelCase__ : List[Any] = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/unclip/karlo_v1_alpha_cat_variation_fp16.npy' ) lowerCAmelCase__ : Tuple = UnCLIPImageVariationPipeline.from_pretrained( 'kakaobrain/karlo-v1-alpha-image-variations' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Union[str, Any] = pipeline.to(a ) pipeline.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = torch.Generator(device='cpu' ).manual_seed(0 ) lowerCAmelCase__ : List[str] = pipeline( a , generator=a , output_type='np' , ) lowerCAmelCase__ : Union[str, Any] = output.images[0] assert image.shape == (256, 256, 3) assert_mean_pixel_difference(a , a , 15 )
307
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available lowerCamelCase__ = { '''configuration_gpt_neo''': ['''GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTNeoConfig''', '''GPTNeoOnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ '''GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTNeoForCausalLM''', '''GPTNeoForQuestionAnswering''', '''GPTNeoForSequenceClassification''', '''GPTNeoForTokenClassification''', '''GPTNeoModel''', '''GPTNeoPreTrainedModel''', '''load_tf_weights_in_gpt_neo''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ '''FlaxGPTNeoForCausalLM''', '''FlaxGPTNeoModel''', '''FlaxGPTNeoPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_neo import GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoConfig, GPTNeoOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neo import ( GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoForCausalLM, GPTNeoForQuestionAnswering, GPTNeoForSequenceClassification, GPTNeoForTokenClassification, GPTNeoModel, GPTNeoPreTrainedModel, load_tf_weights_in_gpt_neo, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_gpt_neo import FlaxGPTNeoForCausalLM, FlaxGPTNeoModel, FlaxGPTNeoPreTrainedModel else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
364
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: stooge(SCREAMING_SNAKE_CASE_ , 0 , len(SCREAMING_SNAKE_CASE_ ) - 1 ) return arr def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowerCAmelCase__ , lowerCAmelCase__ : Tuple = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowerCAmelCase__ : Union[str, Any] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) # Recursively sort last 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , i + t , (SCREAMING_SNAKE_CASE_) ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) if __name__ == "__main__": lowerCamelCase__ = input("""Enter numbers separated by a comma:\n""").strip() lowerCamelCase__ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
307
0
import argparse import glob import logging import os import time from argparse import Namespace import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from torch.utils.data import DataLoader, TensorDataset from transformers import glue_compute_metrics as compute_metrics from transformers import glue_convert_examples_to_features as convert_examples_to_features from transformers import glue_output_modes, glue_tasks_num_labels from transformers import glue_processors as processors lowerCamelCase__ = logging.getLogger(__name__) class A__ ( snake_case__ ): lowercase = 'sequence-classification' def __init__( self : int , a : List[str] ): if type(UpperCAmelCase_ ) == dict: lowerCAmelCase__ : List[Any] = Namespace(**UpperCAmelCase_ ) lowerCAmelCase__ : Union[str, Any] = glue_output_modes[hparams.task] lowerCAmelCase__ : List[str] = glue_tasks_num_labels[hparams.task] super().__init__(UpperCAmelCase_ , UpperCAmelCase_ , self.mode ) def _lowerCamelCase ( self : Any , **a : Optional[int] ): return self.model(**UpperCAmelCase_ ) def _lowerCamelCase ( self : List[str] , a : Optional[int] , a : Dict ): lowerCAmelCase__ : Dict = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: lowerCAmelCase__ : Tuple = batch[2] if self.config.model_type in ["bert", "xlnet", "albert"] else None lowerCAmelCase__ : Any = self(**UpperCAmelCase_ ) lowerCAmelCase__ : Union[str, Any] = outputs[0] lowerCAmelCase__ : Any = self.trainer.lr_schedulers[0]["scheduler"] lowerCAmelCase__ : str = {"loss": loss, "rate": lr_scheduler.get_last_lr()[-1]} return {"loss": loss, "log": tensorboard_logs} def _lowerCamelCase ( self : Tuple ): lowerCAmelCase__ : List[str] = self.hparams lowerCAmelCase__ : int = processors[args.task]() lowerCAmelCase__ : Any = processor.get_labels() for mode in ["train", "dev"]: lowerCAmelCase__ : Tuple = self._feature_file(UpperCAmelCase_ ) if os.path.exists(UpperCAmelCase_ ) and not args.overwrite_cache: logger.info('Loading features from cached file %s' , UpperCAmelCase_ ) else: logger.info('Creating features from dataset file at %s' , args.data_dir ) lowerCAmelCase__ : Optional[Any] = ( processor.get_dev_examples(args.data_dir ) if mode == "dev" else processor.get_train_examples(args.data_dir ) ) lowerCAmelCase__ : List[Any] = convert_examples_to_features( UpperCAmelCase_ , self.tokenizer , max_length=args.max_seq_length , label_list=self.labels , output_mode=args.glue_output_mode , ) logger.info('Saving features into cached file %s' , UpperCAmelCase_ ) torch.save(UpperCAmelCase_ , UpperCAmelCase_ ) def _lowerCamelCase ( self : Dict , a : str , a : int , a : bool = False ): lowerCAmelCase__ : Union[str, Any] = "dev" if mode == "test" else mode lowerCAmelCase__ : int = self._feature_file(UpperCAmelCase_ ) logger.info('Loading features from cached file %s' , UpperCAmelCase_ ) lowerCAmelCase__ : Union[str, Any] = torch.load(UpperCAmelCase_ ) lowerCAmelCase__ : List[Any] = torch.tensor([f.input_ids for f in features] , dtype=torch.long ) lowerCAmelCase__ : Any = torch.tensor([f.attention_mask for f in features] , dtype=torch.long ) lowerCAmelCase__ : int = torch.tensor([f.token_type_ids for f in features] , dtype=torch.long ) if self.hparams.glue_output_mode == "classification": lowerCAmelCase__ : List[str] = torch.tensor([f.label for f in features] , dtype=torch.long ) elif self.hparams.glue_output_mode == "regression": lowerCAmelCase__ : List[Any] = torch.tensor([f.label for f in features] , dtype=torch.float ) return DataLoader( TensorDataset(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) , batch_size=UpperCAmelCase_ , shuffle=UpperCAmelCase_ , ) def _lowerCamelCase ( self : Optional[int] , a : Optional[Any] , a : Tuple ): lowerCAmelCase__ : Tuple = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: lowerCAmelCase__ : Optional[Any] = batch[2] if self.config.model_type in ["bert", "xlnet", "albert"] else None lowerCAmelCase__ : Any = self(**UpperCAmelCase_ ) lowerCAmelCase__ : Dict = outputs[:2] lowerCAmelCase__ : Union[str, Any] = logits.detach().cpu().numpy() lowerCAmelCase__ : Optional[Any] = inputs["labels"].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def _lowerCamelCase ( self : Optional[int] , a : int ): lowerCAmelCase__ : Optional[int] = torch.stack([x['val_loss'] for x in outputs] ).mean().detach().cpu().item() lowerCAmelCase__ : Tuple = np.concatenate([x['pred'] for x in outputs] , axis=0 ) if self.hparams.glue_output_mode == "classification": lowerCAmelCase__ : Optional[Any] = np.argmax(UpperCAmelCase_ , axis=1 ) elif self.hparams.glue_output_mode == "regression": lowerCAmelCase__ : Optional[int] = np.squeeze(UpperCAmelCase_ ) lowerCAmelCase__ : List[str] = np.concatenate([x['target'] for x in outputs] , axis=0 ) lowerCAmelCase__ : List[Any] = [[] for _ in range(out_label_ids.shape[0] )] lowerCAmelCase__ : str = [[] for _ in range(out_label_ids.shape[0] )] lowerCAmelCase__ : Optional[int] = {**{"val_loss": val_loss_mean}, **compute_metrics(self.hparams.task , UpperCAmelCase_ , UpperCAmelCase_ )} lowerCAmelCase__ : int = dict(results.items() ) lowerCAmelCase__ : Union[str, Any] = results return ret, preds_list, out_label_list def _lowerCamelCase ( self : Any , a : list ): lowerCAmelCase__ : str = self._eval_end(UpperCAmelCase_ ) lowerCAmelCase__ : List[Any] = ret["log"] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def _lowerCamelCase ( self : Union[str, Any] , a : Tuple ): lowerCAmelCase__ : int = self._eval_end(UpperCAmelCase_ ) lowerCAmelCase__ : Union[str, Any] = ret["log"] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def _lowerCamelCase ( a : str , a : Union[str, Any] ): BaseTransformer.add_model_specific_args(UpperCAmelCase_ , UpperCAmelCase_ ) parser.add_argument( '--max_seq_length' , default=128 , type=UpperCAmelCase_ , help=( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) , ) parser.add_argument( '--task' , default='' , type=UpperCAmelCase_ , required=UpperCAmelCase_ , help='The GLUE task to run' , ) parser.add_argument( '--gpus' , default=0 , type=UpperCAmelCase_ , help='The number of GPUs allocated for this, it is by default 0 meaning none' , ) parser.add_argument( '--overwrite_cache' , action='store_true' , help='Overwrite the cached training and evaluation sets' ) return parser def lowerCAmelCase__ ( ) -> Any: lowerCAmelCase__ : Optional[Any] = argparse.ArgumentParser() add_generic_args(_UpperCAmelCase , os.getcwd() ) lowerCAmelCase__ : Optional[Any] = GLUETransformer.add_model_specific_args(_UpperCAmelCase , os.getcwd() ) lowerCAmelCase__ : str = parser.parse_args() # If output_dir not provided, a folder will be generated in pwd if args.output_dir is None: lowerCAmelCase__ : int = os.path.join( './results' , F'''{args.task}_{time.strftime("%Y%m%d_%H%M%S" )}''' , ) os.makedirs(args.output_dir ) lowerCAmelCase__ : List[Any] = GLUETransformer(_UpperCAmelCase ) lowerCAmelCase__ : List[str] = generic_train(_UpperCAmelCase , _UpperCAmelCase ) # Optionally, predict on dev set and write to output_dir if args.do_predict: lowerCAmelCase__ : str = sorted(glob.glob(os.path.join(args.output_dir , 'checkpoint-epoch=*.ckpt' ) , recursive=_UpperCAmelCase ) ) lowerCAmelCase__ : Any = model.load_from_checkpoint(checkpoints[-1] ) return trainer.test(_UpperCAmelCase ) if __name__ == "__main__": main()
365
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) lowerCamelCase__ = { """configuration_speech_to_text""": ["""SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Speech2TextConfig"""], """processing_speech_to_text""": ["""Speech2TextProcessor"""], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextTokenizer"""] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextFeatureExtractor"""] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFSpeech2TextForConditionalGeneration""", """TFSpeech2TextModel""", """TFSpeech2TextPreTrainedModel""", ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """Speech2TextForConditionalGeneration""", """Speech2TextModel""", """Speech2TextPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ = {"""configuration_vit_msn""": ["""VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ViTMSNConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST""", """ViTMSNModel""", """ViTMSNForImageClassification""", """ViTMSNPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
366
import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = { 'task_specific_params': { 'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4}, 'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4}, 'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6}, } } lowerCAmelCase__ : int = { 'task_specific_params.summarization.length_penalty': 1.0, 'task_specific_params.summarization.max_length': 128, 'task_specific_params.summarization.min_length': 12, 'task_specific_params.summarization.num_beams': 4, 'task_specific_params.summarization_cnn.length_penalty': 2.0, 'task_specific_params.summarization_cnn.max_length': 142, 'task_specific_params.summarization_cnn.min_length': 56, 'task_specific_params.summarization_cnn.num_beams': 4, 'task_specific_params.summarization_xsum.length_penalty': 1.0, 'task_specific_params.summarization_xsum.max_length': 62, 'task_specific_params.summarization_xsum.min_length': 11, 'task_specific_params.summarization_xsum.num_beams': 6, } self.assertEqual(flatten_dict(a ) , a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(a ) , x.transpose() ) ) lowerCAmelCase__ : List[str] = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : int = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Dict = tf.constant(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : int = jnp.array(a ) self.assertTrue(np.allclose(transpose(a ) , np.asarray(transpose(a ) ) ) ) lowerCAmelCase__ : Any = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = jnp.array(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , np.asarray(transpose(a , axes=(1, 2, 0) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.reshape(a , (4, 3) ) ) ) lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.reshape(a , (12, 5) ) ) ) @require_torch def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : Dict = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[str] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.asarray(reshape(a , (4, 3) ) ) ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.asarray(reshape(a , (12, 5) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(a ) , np.squeeze(a ) ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.squeeze(a , axis=2 ) ) ) @require_torch def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : Optional[Any] = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : str = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a ) , np.asarray(squeeze(a ) ) ) ) lowerCAmelCase__ : str = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Optional[Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.asarray(squeeze(a , axis=2 ) ) ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.expand_dims(a , axis=1 ) ) ) @require_torch def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : str = np.random.randn(3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = np.random.randn(3 , 4 ) lowerCAmelCase__ : Tuple = jnp.array(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.asarray(expand_dims(a , axis=1 ) ) ) )
307
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowerCamelCase__ = { 'configuration_vision_encoder_decoder': ['VisionEncoderDecoderConfig', 'VisionEncoderDecoderOnnxConfig'] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ['VisionEncoderDecoderModel'] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ['TFVisionEncoderDecoderModel'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ['FlaxVisionEncoderDecoderModel'] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
367
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position lowerCamelCase__ = """2.13.1""" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("""3.7"""): raise ImportWarning( """To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition.""" ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( """To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n""" """If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.""" ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip lowerCamelCase__ = concatenate_datasets lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadManager lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
307
0
import glob import os import random from string import ascii_lowercase, digits import cva import numpy as np # Parrameters lowerCamelCase__ = (720, 1280) # Height, Width lowerCamelCase__ = (0.4, 0.6) # if height or width lower than this scale, drop it. lowerCamelCase__ = 1 / 100 lowerCamelCase__ = """""" lowerCamelCase__ = """""" lowerCamelCase__ = """""" lowerCamelCase__ = 250 def lowerCAmelCase__ ( ) -> None: lowerCAmelCase__ , lowerCAmelCase__ : Any = get_dataset(__lowerCamelCase , __lowerCamelCase ) for index in range(__lowerCamelCase ): lowerCAmelCase__ : Dict = random.sample(range(len(__lowerCamelCase ) ) , 4 ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = update_image_and_anno( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , filter_scale=__lowerCamelCase , ) # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' lowerCAmelCase__ : List[Any] = random_chars(32 ) lowerCAmelCase__ : Any = path.split(os.sep )[-1].rsplit('.' , 1 )[0] lowerCAmelCase__ : Union[str, Any] = F'''{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}''' cva.imwrite(F'''{file_root}.jpg''' , __lowerCamelCase , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F'''Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}''' ) lowerCAmelCase__ : List[Any] = [] for anno in new_annos: lowerCAmelCase__ : List[Any] = anno[3] - anno[1] lowerCAmelCase__ : Optional[int] = anno[4] - anno[2] lowerCAmelCase__ : str = anno[1] + width / 2 lowerCAmelCase__ : Optional[int] = anno[2] + height / 2 lowerCAmelCase__ : Any = F'''{anno[0]} {x_center} {y_center} {width} {height}''' annos_list.append(__lowerCamelCase ) with open(F'''{file_root}.txt''' , 'w' ) as outfile: outfile.write('\n'.join(line for line in annos_list ) ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> tuple[list, list]: lowerCAmelCase__ : List[Any] = [] lowerCAmelCase__ : List[Any] = [] for label_file in glob.glob(os.path.join(__lowerCamelCase , '*.txt' ) ): lowerCAmelCase__ : Any = label_file.split(os.sep )[-1].rsplit('.' , 1 )[0] with open(__lowerCamelCase ) as in_file: lowerCAmelCase__ : int = in_file.readlines() lowerCAmelCase__ : List[Any] = os.path.join(__lowerCamelCase , F'''{label_name}.jpg''' ) lowerCAmelCase__ : List[Any] = [] for obj_list in obj_lists: lowerCAmelCase__ : Optional[int] = obj_list.rstrip('\n' ).split(' ' ) lowerCAmelCase__ : int = float(obj[1] ) - float(obj[3] ) / 2 lowerCAmelCase__ : List[str] = float(obj[2] ) - float(obj[4] ) / 2 lowerCAmelCase__ : Dict = float(obj[1] ) + float(obj[3] ) / 2 lowerCAmelCase__ : Tuple = float(obj[2] ) + float(obj[4] ) / 2 boxes.append([int(obj[0] ), xmin, ymin, xmax, ymax] ) if not boxes: continue img_paths.append(__lowerCamelCase ) labels.append(__lowerCamelCase ) return img_paths, labels def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 0.0 , ) -> tuple[list, list, str]: lowerCAmelCase__ : Tuple = np.zeros([output_size[0], output_size[1], 3] , dtype=np.uinta ) lowerCAmelCase__ : Optional[int] = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) lowerCAmelCase__ : Optional[int] = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) lowerCAmelCase__ : int = int(scale_x * output_size[1] ) lowerCAmelCase__ : List[str] = int(scale_y * output_size[0] ) lowerCAmelCase__ : List[str] = [] lowerCAmelCase__ : Tuple = [] for i, index in enumerate(__lowerCamelCase ): lowerCAmelCase__ : List[str] = all_img_list[index] path_list.append(__lowerCamelCase ) lowerCAmelCase__ : List[Any] = all_annos[index] lowerCAmelCase__ : List[str] = cva.imread(__lowerCamelCase ) if i == 0: # top-left lowerCAmelCase__ : Tuple = cva.resize(__lowerCamelCase , (divid_point_x, divid_point_y) ) lowerCAmelCase__ : Tuple = img for bbox in img_annos: lowerCAmelCase__ : str = bbox[1] * scale_x lowerCAmelCase__ : int = bbox[2] * scale_y lowerCAmelCase__ : Dict = bbox[3] * scale_x lowerCAmelCase__ : Dict = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 1: # top-right lowerCAmelCase__ : Union[str, Any] = cva.resize(__lowerCamelCase , (output_size[1] - divid_point_x, divid_point_y) ) lowerCAmelCase__ : Union[str, Any] = img for bbox in img_annos: lowerCAmelCase__ : Optional[int] = scale_x + bbox[1] * (1 - scale_x) lowerCAmelCase__ : List[Any] = bbox[2] * scale_y lowerCAmelCase__ : Optional[int] = scale_x + bbox[3] * (1 - scale_x) lowerCAmelCase__ : List[str] = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 2: # bottom-left lowerCAmelCase__ : Union[str, Any] = cva.resize(__lowerCamelCase , (divid_point_x, output_size[0] - divid_point_y) ) lowerCAmelCase__ : List[str] = img for bbox in img_annos: lowerCAmelCase__ : Union[str, Any] = bbox[1] * scale_x lowerCAmelCase__ : Tuple = scale_y + bbox[2] * (1 - scale_y) lowerCAmelCase__ : List[Any] = bbox[3] * scale_x lowerCAmelCase__ : Optional[int] = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) else: # bottom-right lowerCAmelCase__ : List[Any] = cva.resize( __lowerCamelCase , (output_size[1] - divid_point_x, output_size[0] - divid_point_y) ) lowerCAmelCase__ : Optional[Any] = img for bbox in img_annos: lowerCAmelCase__ : str = scale_x + bbox[1] * (1 - scale_x) lowerCAmelCase__ : Optional[int] = scale_y + bbox[2] * (1 - scale_y) lowerCAmelCase__ : List[Any] = scale_x + bbox[3] * (1 - scale_x) lowerCAmelCase__ : Union[str, Any] = 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: lowerCAmelCase__ : int = [ 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__ ( SCREAMING_SNAKE_CASE_ ) -> str: assert number_char > 1, "The number of character should greater than 1" lowerCAmelCase__ : Optional[int] = ascii_lowercase + digits return "".join(random.choice(__lowerCamelCase ) for _ in range(__lowerCamelCase ) ) if __name__ == "__main__": main() print("""DONE ✅""")
368
import gc import unittest import numpy as np import torch from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = DanceDiffusionPipeline lowercase = UNCONDITIONAL_AUDIO_GENERATION_PARAMS lowercase = PipelineTesterMixin.required_optional_params - { 'callback', 'latents', 'callback_steps', 'output_type', 'num_images_per_prompt', } lowercase = UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS lowercase = False lowercase = False def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = UNetaDModel( block_out_channels=(32, 32, 64) , extra_in_channels=16 , sample_size=512 , sample_rate=16_000 , in_channels=2 , out_channels=2 , flip_sin_to_cos=a , use_timestep_embedding=a , time_embedding_type='fourier' , mid_block_type='UNetMidBlock1D' , down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D') , up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip') , ) lowerCAmelCase__ : Tuple = IPNDMScheduler() lowerCAmelCase__ : str = { 'unet': unet, 'scheduler': scheduler, } return components def _lowerCamelCase ( self : int , a : Dict , a : List[str]=0 ): '''simple docstring''' if str(a ).startswith('mps' ): lowerCAmelCase__ : Union[str, Any] = torch.manual_seed(a ) else: lowerCAmelCase__ : Optional[Any] = torch.Generator(device=a ).manual_seed(a ) lowerCAmelCase__ : Optional[Any] = { 'batch_size': 1, 'generator': generator, 'num_inference_steps': 4, } return inputs def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'cpu' # ensure determinism for the device-dependent torch.Generator lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : List[str] = DanceDiffusionPipeline(**a ) lowerCAmelCase__ : Any = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a ) lowerCAmelCase__ : List[Any] = pipe(**a ) lowerCAmelCase__ : List[str] = output.audios lowerCAmelCase__ : Optional[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, components["unet"].sample_size) lowerCAmelCase__ : List[Any] = np.array([-0.7_2_6_5, 1.0_0_0_0, -0.8_3_8_8, 0.1_1_7_5, 0.9_4_9_8, -1.0_0_0_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) @skip_mps def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return super().test_save_load_optional_components() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_attention_slicing_forward_pass() def _lowerCamelCase ( self : List[str] ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Dict ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device lowerCAmelCase__ : List[str] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' ) lowerCAmelCase__ : List[str] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : int = output.audios lowerCAmelCase__ : List[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : Dict = np.array([-0.0_1_9_2, -0.0_2_3_1, -0.0_3_1_8, -0.0_0_5_9, 0.0_0_0_2, -0.0_0_2_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = torch_device lowerCAmelCase__ : List[Any] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Optional[int] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : str = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : str = output.audios lowerCAmelCase__ : Tuple = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : int = np.array([-0.0_3_6_7, -0.0_4_8_8, -0.0_7_7_1, -0.0_5_2_5, -0.0_4_4_4, -0.0_3_4_1] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2
307
0
import argparse from pathlib import Path from typing import Dict, OrderedDict, Tuple import torch from audiocraft.models import MusicGen from transformers import ( AutoFeatureExtractor, AutoTokenizer, EncodecModel, MusicgenDecoderConfig, MusicgenForConditionalGeneration, MusicgenProcessor, TaEncoderModel, ) from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = ["model.decoder.embed_positions.weights"] def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: if "emb" in name: lowerCAmelCase__ : Optional[Any] = name.replace('emb' , 'model.decoder.embed_tokens' ) if "transformer" in name: lowerCAmelCase__ : List[str] = name.replace('transformer' , 'model.decoder' ) if "cross_attention" in name: lowerCAmelCase__ : Dict = name.replace('cross_attention' , 'encoder_attn' ) if "linear1" in name: lowerCAmelCase__ : Optional[Any] = name.replace('linear1' , 'fc1' ) if "linear2" in name: lowerCAmelCase__ : List[str] = name.replace('linear2' , 'fc2' ) if "norm1" in name: lowerCAmelCase__ : Optional[int] = name.replace('norm1' , 'self_attn_layer_norm' ) if "norm_cross" in name: lowerCAmelCase__ : Union[str, Any] = name.replace('norm_cross' , 'encoder_attn_layer_norm' ) if "norm2" in name: lowerCAmelCase__ : Any = name.replace('norm2' , 'final_layer_norm' ) if "out_norm" in name: lowerCAmelCase__ : Union[str, Any] = name.replace('out_norm' , 'model.decoder.layer_norm' ) if "linears" in name: lowerCAmelCase__ : Optional[Any] = name.replace('linears' , 'lm_heads' ) if "condition_provider.conditioners.description.output_proj" in name: lowerCAmelCase__ : Any = name.replace('condition_provider.conditioners.description.output_proj' , 'enc_to_dec_proj' ) return name def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple[Dict, Dict]: lowerCAmelCase__ : int = list(state_dict.keys() ) lowerCAmelCase__ : Dict = {} for key in keys: lowerCAmelCase__ : Any = state_dict.pop(_snake_case ) lowerCAmelCase__ : Optional[Any] = rename_keys(_snake_case ) if "in_proj_weight" in key: # split fused qkv proj lowerCAmelCase__ : Optional[int] = val[:hidden_size, :] lowerCAmelCase__ : List[str] = val[hidden_size : 2 * hidden_size, :] lowerCAmelCase__ : List[str] = val[-hidden_size:, :] elif "enc_to_dec_proj" in key: lowerCAmelCase__ : int = val else: lowerCAmelCase__ : str = val return state_dict, enc_dec_proj_state_dict def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> MusicgenDecoderConfig: if checkpoint == "small": # default config values lowerCAmelCase__ : Tuple = 1_024 lowerCAmelCase__ : List[str] = 24 lowerCAmelCase__ : str = 16 elif checkpoint == "medium": lowerCAmelCase__ : Optional[int] = 1_536 lowerCAmelCase__ : Dict = 48 lowerCAmelCase__ : List[Any] = 24 elif checkpoint == "large": lowerCAmelCase__ : Any = 2_048 lowerCAmelCase__ : int = 48 lowerCAmelCase__ : str = 32 else: raise ValueError(F'''Checkpoint should be one of `[\'small\', \'medium\', \'large\']`, got {checkpoint}.''' ) lowerCAmelCase__ : str = MusicgenDecoderConfig( hidden_size=_snake_case , ffn_dim=hidden_size * 4 , num_hidden_layers=_snake_case , num_attention_heads=_snake_case , ) return config @torch.no_grad() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_="cpu" ) -> List[str]: lowerCAmelCase__ : Dict = MusicGen.get_pretrained(_snake_case , device=_snake_case ) lowerCAmelCase__ : Any = decoder_config_from_checkpoint(_snake_case ) lowerCAmelCase__ : Any = fairseq_model.lm.state_dict() lowerCAmelCase__ : Optional[Any] = rename_state_dict( _snake_case , hidden_size=decoder_config.hidden_size ) lowerCAmelCase__ : str = TaEncoderModel.from_pretrained('t5-base' ) lowerCAmelCase__ : Any = EncodecModel.from_pretrained('facebook/encodec_32khz' ) lowerCAmelCase__ : int = MusicgenForCausalLM(_snake_case ).eval() # load all decoder weights - expect that we'll be missing embeddings and enc-dec projection lowerCAmelCase__ : List[str] = decoder.load_state_dict(_snake_case , strict=_snake_case ) for key in missing_keys.copy(): if key.startswith(('text_encoder', 'audio_encoder') ) or key in EXPECTED_MISSING_KEYS: missing_keys.remove(_snake_case ) if len(_snake_case ) > 0: raise ValueError(F'''Missing key(s) in state_dict: {missing_keys}''' ) if len(_snake_case ) > 0: raise ValueError(F'''Unexpected key(s) in state_dict: {unexpected_keys}''' ) # init the composite model lowerCAmelCase__ : Optional[Any] = MusicgenForConditionalGeneration(text_encoder=_snake_case , audio_encoder=_snake_case , decoder=_snake_case ) # load the pre-trained enc-dec projection (from the decoder state dict) model.enc_to_dec_proj.load_state_dict(_snake_case ) # check we can do a forward pass lowerCAmelCase__ : Optional[Any] = torch.arange(0 , 8 , dtype=torch.long ).reshape(2 , -1 ) lowerCAmelCase__ : List[Any] = input_ids.reshape(2 * 4 , -1 ) with torch.no_grad(): lowerCAmelCase__ : Dict = model(input_ids=_snake_case , decoder_input_ids=_snake_case ).logits if logits.shape != (8, 1, 2_048): raise ValueError('Incorrect shape for logits' ) # now construct the processor lowerCAmelCase__ : Optional[Any] = AutoTokenizer.from_pretrained('t5-base' ) lowerCAmelCase__ : List[str] = AutoFeatureExtractor.from_pretrained('facebook/encodec_32khz' , padding_side='left' ) lowerCAmelCase__ : Union[str, Any] = MusicgenProcessor(feature_extractor=_snake_case , tokenizer=_snake_case ) # set the appropriate bos/pad token ids lowerCAmelCase__ : List[str] = 2_048 lowerCAmelCase__ : List[str] = 2_048 # set other default generation config params lowerCAmelCase__ : Union[str, Any] = int(30 * audio_encoder.config.frame_rate ) lowerCAmelCase__ : Optional[Any] = True lowerCAmelCase__ : Dict = 3.0 if pytorch_dump_folder is not None: Path(_snake_case ).mkdir(exist_ok=_snake_case ) logger.info(F'''Saving model {checkpoint} to {pytorch_dump_folder}''' ) model.save_pretrained(_snake_case ) processor.save_pretrained(_snake_case ) if repo_id: logger.info(F'''Pushing model {checkpoint} to {repo_id}''' ) model.push_to_hub(_snake_case ) processor.push_to_hub(_snake_case ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint""", default="""small""", type=str, help="""Checkpoint size of the MusicGen model you'd like to convert. Can be one of: `['small', 'medium', 'large']`.""", ) parser.add_argument( """--pytorch_dump_folder""", required=True, default=None, type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument( """--push_to_hub""", default=None, type=str, help="""Where to upload the converted model on the 🤗 hub.""" ) parser.add_argument( """--device""", default="""cpu""", type=str, help="""Torch device to run the conversion, either cpu or cuda.""" ) lowerCamelCase__ = parser.parse_args() convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)
369
from ..utils import DummyObject, requires_backends class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Union[str, Any] , *a : Optional[int] , **a : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : List[Any] , **a : int ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Any , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Optional[Any] , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Optional[int] , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : List[Any] , *a : List[str] , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Union[str, Any] , **a : Optional[int] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : List[Any] , *a : Dict , **a : List[str] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Dict , **a : List[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Dict , *a : str , **a : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Union[str, Any] , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : Union[str, Any] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Tuple , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] )
307
0
import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets lowerCamelCase__ = datasets.logging.get_logger(__name__) lowerCamelCase__ = """\ @InProceedings{moosavi2019minimum, author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube}, title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection}, year = {2019}, booktitle = {Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)}, publisher = {Association for Computational Linguistics}, address = {Florence, Italy}, } @inproceedings{10.3115/1072399.1072405, author = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette}, title = {A Model-Theoretic Coreference Scoring Scheme}, year = {1995}, isbn = {1558604022}, publisher = {Association for Computational Linguistics}, address = {USA}, url = {https://doi.org/10.3115/1072399.1072405}, doi = {10.3115/1072399.1072405}, booktitle = {Proceedings of the 6th Conference on Message Understanding}, pages = {45–52}, numpages = {8}, location = {Columbia, Maryland}, series = {MUC6 ’95} } @INPROCEEDINGS{Bagga98algorithmsfor, author = {Amit Bagga and Breck Baldwin}, title = {Algorithms for Scoring Coreference Chains}, booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference}, year = {1998}, pages = {563--566} } @INPROCEEDINGS{Luo05oncoreference, author = {Xiaoqiang Luo}, title = {On coreference resolution performance metrics}, booktitle = {In Proc. of HLT/EMNLP}, year = {2005}, pages = {25--32}, publisher = {URL} } @inproceedings{moosavi-strube-2016-coreference, title = \"Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric\", author = \"Moosavi, Nafise Sadat and Strube, Michael\", booktitle = \"Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)\", month = aug, year = \"2016\", address = \"Berlin, Germany\", publisher = \"Association for Computational Linguistics\", url = \"https://www.aclweb.org/anthology/P16-1060\", doi = \"10.18653/v1/P16-1060\", pages = \"632--642\", } """ lowerCamelCase__ = """\ CoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which implements of the common evaluation metrics including MUC [Vilain et al, 1995], B-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005], LEA [Moosavi and Strube, 2016] and the averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) [Denis and Baldridge, 2009a; Pradhan et al., 2011]. This wrapper of CoVal currently only work with CoNLL line format: The CoNLL format has one word per line with all the annotation for this word in column separated by spaces: Column Type Description 1 Document ID This is a variation on the document filename 2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc. 3 Word number 4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release. 5 Part-of-Speech 6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the \"([pos] [word])\" string (or leaf) and concatenating the items in the rows of that column. 7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a \"-\" 8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7. 9 Word sense This is the word sense of the word in Column 3. 10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data. 11 Named Entities These columns identifies the spans representing various named entities. 12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7. N Coreference Coreference chain information encoded in a parenthesis structure. More informations on the format can be found here (section \"*_conll File Format\"): http://www.conll.cemantix.org/2012/data.html Details on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md CoVal code was written by @ns-moosavi. Some parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py The test suite is taken from https://github.com/conll/reference-coreference-scorers/ Mention evaluation and the test suite are added by @andreasvc. Parsing CoNLL files is developed by Leo Born. """ lowerCamelCase__ = """ Calculates coreference evaluation metrics. Args: predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format. Each prediction is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format. Each reference is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. keep_singletons: After extracting all mentions of key or system files, mentions whose corresponding coreference chain is of size one, are considered as singletons. The default evaluation mode will include singletons in evaluations if they are included in the key or the system files. By setting \'keep_singletons=False\', all singletons in the key and system files will be excluded from the evaluation. NP_only: Most of the recent coreference resolvers only resolve NP mentions and leave out the resolution of VPs. By setting the \'NP_only\' option, the scorer will only evaluate the resolution of NPs. min_span: By setting \'min_span\', the scorer reports the results based on automatically detected minimum spans. Minimum spans are determined using the MINA algorithm. Returns: \'mentions\': mentions \'muc\': MUC metric [Vilain et al, 1995] \'bcub\': B-cubed [Bagga and Baldwin, 1998] \'ceafe\': CEAFe [Luo et al., 2005] \'lea\': LEA [Moosavi and Strube, 2016] \'conll_score\': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) Examples: >>> coval = datasets.load_metric(\'coval\') >>> words = [\'bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -\', ... \'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)\', ... \'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)\', ... \'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -\', ... \'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -\', ... \'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -\'] >>> references = [words] >>> predictions = [words] >>> results = coval.compute(predictions=predictions, references=references) >>> print(results) # doctest:+ELLIPSIS {\'mentions/recall\': 1.0,[...] \'conll_score\': 100.0} """ def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_="dummy_doc" ) -> List[Any]: lowerCAmelCase__ : str = {doc: key_lines} lowerCAmelCase__ : str = {doc: sys_lines} lowerCAmelCase__ : List[Any] = {} lowerCAmelCase__ : Dict = 0 lowerCAmelCase__ : str = 0 lowerCAmelCase__ : Union[str, Any] = 0 lowerCAmelCase__ : Tuple = 0 lowerCAmelCase__ : List[Any] = 0 lowerCAmelCase__ : Union[str, Any] = 0 lowerCAmelCase__ : Dict = reader.get_doc_mentions(SCREAMING_SNAKE_CASE_ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE_ ) key_singletons_num += singletons_num if NP_only or min_span: lowerCAmelCase__ : List[str] = reader.set_annotated_parse_trees(SCREAMING_SNAKE_CASE_ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = reader.get_doc_mentions(SCREAMING_SNAKE_CASE_ , sys_doc_lines[doc] , SCREAMING_SNAKE_CASE_ ) sys_singletons_num += singletons_num if NP_only or min_span: lowerCAmelCase__ : Optional[Any] = reader.set_annotated_parse_trees(SCREAMING_SNAKE_CASE_ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if remove_nested: lowerCAmelCase__ : List[Any] = reader.remove_nested_coref_mentions(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters lowerCAmelCase__ : Dict = reader.remove_nested_coref_mentions(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters lowerCAmelCase__ : Optional[int] = reader.get_mention_assignments(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = reader.get_mention_assignments(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : int = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( 'Number of removed nested coreferring mentions in the key ' F'''annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}''' ) logger.info( 'Number of resulting singleton clusters in the key ' F'''annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}''' ) if not keep_singletons: logger.info( F'''{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system ''' 'files, respectively' ) return doc_coref_infos def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: lowerCAmelCase__ : Union[str, Any] = get_coref_infos(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : str = {} lowerCAmelCase__ : List[str] = 0 lowerCAmelCase__ : Dict = 0 for name, metric in metrics: lowerCAmelCase__ : Dict = evaluator.evaluate_documents(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({F'''{name}/recall''': recall, F'''{name}/precision''': precision, F'''{name}/f1''': fa} ) logger.info( name.ljust(10 ) , F'''Recall: {recall * 100:.2f}''' , F''' Precision: {precision * 100:.2f}''' , F''' F1: {fa * 100:.2f}''' , ) if conll_subparts_num == 3: lowerCAmelCase__ : List[Any] = (conll / 3) * 100 logger.info(F'''CoNLL score: {conll:.2f}''' ) output_scores.update({'conll_score': conll} ) return output_scores def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: lowerCAmelCase__ : Dict = False for line in key_lines: if not line.startswith('#' ): if len(line.split() ) > 6: lowerCAmelCase__ : Any = line.split()[5] if not parse_col == "-": lowerCAmelCase__ : List[str] = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A__ ( datasets.Metric ): def _lowerCamelCase ( self : str ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' ) ), 'references': datasets.Sequence(datasets.Value('string' ) ), } ) , codebase_urls=['https://github.com/ns-moosavi/coval'] , reference_urls=[ 'https://github.com/ns-moosavi/coval', 'https://www.aclweb.org/anthology/P16-1060', 'http://www.conll.cemantix.org/2012/data.html', ] , ) def _lowerCamelCase ( self : Dict , a : Dict , a : int , a : Any=True , a : Optional[Any]=False , a : Optional[Any]=False , a : Any=False ): '''simple docstring''' lowerCAmelCase__ : Dict = [ ("mentions", evaluator.mentions), ("muc", evaluator.muc), ("bcub", evaluator.b_cubed), ("ceafe", evaluator.ceafe), ("lea", evaluator.lea), ] if min_span: lowerCAmelCase__ : List[str] = util.check_gold_parse_annotation(_a ) if not has_gold_parse: raise NotImplementedError('References should have gold parse annotation to use \'min_span\'.' ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" lowerCAmelCase__ : int = evaluate( key_lines=_a , sys_lines=_a , metrics=_a , NP_only=_a , remove_nested=_a , keep_singletons=_a , min_span=_a , ) return score
370
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class A__ : def __init__( self : List[str] , a : Any , a : Dict=13 , a : Optional[Any]=7 , a : Tuple=True , a : Tuple=True , a : Dict=False , a : Optional[Any]=True , a : Dict=99 , a : Tuple=32 , a : Optional[Any]=5 , a : str=4 , a : Union[str, Any]=37 , a : Any="gelu" , a : Dict=0.1 , a : Any=0.1 , a : Optional[int]=512 , a : Union[str, Any]=16 , a : Optional[int]=2 , a : Optional[Any]=0.0_2 , a : List[Any]=3 , a : Any=4 , a : Optional[int]=None , ): '''simple docstring''' lowerCAmelCase__ : List[str] = parent lowerCAmelCase__ : str = batch_size lowerCAmelCase__ : Optional[int] = seq_length lowerCAmelCase__ : Optional[Any] = is_training lowerCAmelCase__ : Tuple = use_input_mask lowerCAmelCase__ : List[Any] = use_token_type_ids lowerCAmelCase__ : str = use_labels lowerCAmelCase__ : Dict = vocab_size lowerCAmelCase__ : Union[str, Any] = hidden_size lowerCAmelCase__ : Optional[int] = num_hidden_layers lowerCAmelCase__ : List[Any] = num_attention_heads lowerCAmelCase__ : int = intermediate_size lowerCAmelCase__ : Union[str, Any] = hidden_act lowerCAmelCase__ : Union[str, Any] = hidden_dropout_prob lowerCAmelCase__ : Any = attention_probs_dropout_prob lowerCAmelCase__ : Dict = max_position_embeddings lowerCAmelCase__ : int = type_vocab_size lowerCAmelCase__ : int = type_sequence_label_size lowerCAmelCase__ : Dict = initializer_range lowerCAmelCase__ : List[str] = num_labels lowerCAmelCase__ : Any = num_choices lowerCAmelCase__ : List[Any] = scope def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase__ : Tuple = None if self.use_input_mask: lowerCAmelCase__ : str = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase__ : List[str] = None if self.use_token_type_ids: lowerCAmelCase__ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCAmelCase__ : Optional[Any] = None lowerCAmelCase__ : Dict = None lowerCAmelCase__ : str = None if self.use_labels: lowerCAmelCase__ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCAmelCase__ : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) lowerCAmelCase__ : List[str] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return LlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=a , initializer_range=self.initializer_range , ) def _lowerCamelCase ( self : Tuple , a : Dict , a : List[str] , a : str , a : Union[str, Any] , a : Optional[Any] , a : Dict , a : str ): '''simple docstring''' lowerCAmelCase__ : str = LlamaModel(config=a ) model.to(a ) model.eval() lowerCAmelCase__ : Dict = model(a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCamelCase ( self : int , a : Any , a : Union[str, Any] , a : Dict , a : Dict , a : List[Any] , a : Optional[Any] , a : int , a : Dict , a : Tuple , ): '''simple docstring''' lowerCAmelCase__ : int = True lowerCAmelCase__ : Dict = LlamaModel(a ) model.to(a ) model.eval() lowerCAmelCase__ : List[Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , ) lowerCAmelCase__ : Optional[int] = model( a , attention_mask=a , encoder_hidden_states=a , ) lowerCAmelCase__ : Union[str, Any] = model(a , attention_mask=a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCamelCase ( self : Union[str, Any] , a : int , a : List[Any] , a : int , a : Tuple , a : List[Any] , a : Union[str, Any] , a : Any , a : List[str] , a : List[str] , ): '''simple docstring''' lowerCAmelCase__ : List[Any] = LlamaForCausalLM(config=a ) model.to(a ) model.eval() lowerCAmelCase__ : Tuple = model(a , attention_mask=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowerCamelCase ( self : str , a : Any , a : Tuple , a : str , a : Union[str, Any] , a : Optional[Any] , a : List[Any] , a : Optional[Any] , a : Optional[Any] , a : List[str] , ): '''simple docstring''' lowerCAmelCase__ : str = True lowerCAmelCase__ : Optional[int] = True lowerCAmelCase__ : List[Any] = LlamaForCausalLM(config=a ) model.to(a ) model.eval() # first forward pass lowerCAmelCase__ : List[str] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , use_cache=a , ) lowerCAmelCase__ : List[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids lowerCAmelCase__ : Union[str, Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowerCAmelCase__ : Tuple = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and lowerCAmelCase__ : int = torch.cat([input_ids, next_tokens] , dim=-1 ) lowerCAmelCase__ : Any = torch.cat([input_mask, next_mask] , dim=-1 ) lowerCAmelCase__ : Union[str, Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , output_hidden_states=a , )['hidden_states'][0] lowerCAmelCase__ : Union[str, Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , past_key_values=a , output_hidden_states=a , )['hidden_states'][0] # select random slice lowerCAmelCase__ : Any = ids_tensor((1,) , output_from_past.shape[-1] ).item() lowerCAmelCase__ : Optional[int] = output_from_no_past[:, -3:, random_slice_idx].detach() lowerCAmelCase__ : Any = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(a , a , atol=1E-3 ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.prepare_config_and_inputs() ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : Any = config_and_inputs lowerCAmelCase__ : List[str] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class A__ ( __magic_name__ , __magic_name__ , __magic_name__ , unittest.TestCase ): lowercase = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () lowercase = (LlamaForCausalLM,) if is_torch_available() else () lowercase = ( { 'feature-extraction': LlamaModel, 'text-classification': LlamaForSequenceClassification, 'text-generation': LlamaForCausalLM, 'zero-shot': LlamaForSequenceClassification, } if is_torch_available() else {} ) lowercase = False lowercase = False def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Tuple = LlamaModelTester(self ) lowerCAmelCase__ : str = ConfigTester(self , config_class=a , hidden_size=37 ) def _lowerCamelCase ( self : str ): '''simple docstring''' self.config_tester.run_common_tests() def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCAmelCase__ : int = type self.model_tester.create_and_check_model(*a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : int = 3 lowerCAmelCase__ : Dict = input_dict['input_ids'] lowerCAmelCase__ : Optional[Any] = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Tuple = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : Tuple = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : str = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : List[Any] = 3 lowerCAmelCase__ : List[str] = 'single_label_classification' lowerCAmelCase__ : List[Any] = input_dict['input_ids'] lowerCAmelCase__ : List[Any] = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Optional[int] = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : int = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : Optional[int] = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Optional[Any] = 3 lowerCAmelCase__ : Optional[Any] = 'multi_label_classification' lowerCAmelCase__ : List[str] = input_dict['input_ids'] lowerCAmelCase__ : Tuple = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Any = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) lowerCAmelCase__ : Dict = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : Dict = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' pass @parameterized.expand([('linear',), ('dynamic',)] ) def _lowerCamelCase ( self : Optional[int] , a : Dict ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Tuple = ids_tensor([1, 10] , config.vocab_size ) lowerCAmelCase__ : List[str] = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : List[Any] = LlamaModel(a ) original_model.to(a ) original_model.eval() lowerCAmelCase__ : List[Any] = original_model(a ).last_hidden_state lowerCAmelCase__ : str = original_model(a ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : Any = {'type': scaling_type, 'factor': 1_0.0} lowerCAmelCase__ : Union[str, Any] = LlamaModel(a ) scaled_model.to(a ) scaled_model.eval() lowerCAmelCase__ : Union[str, Any] = scaled_model(a ).last_hidden_state lowerCAmelCase__ : Optional[int] = scaled_model(a ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(a , a , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(a , a , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(a , a , atol=1E-5 ) ) @require_torch class A__ ( unittest.TestCase ): @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' , device_map='auto' ) lowerCAmelCase__ : Any = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 lowerCAmelCase__ : Dict = torch.tensor([[-6.6_5_5_0, -4.1_2_2_7, -4.9_8_5_9, -3.2_4_0_6, 0.8_2_6_2, -3.0_0_3_3, 1.2_9_6_4, -3.3_6_9_9]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : List[Any] = torch.tensor([-1_2.8_2_8_1, -7.4_4_5_3, -0.4_6_3_9, -8.0_6_2_5, -7.2_5_0_0, -8.0_0_0_0, -6.4_8_8_3, -7.7_6_9_5, -7.8_4_3_8, -7.0_3_1_2, -6.2_1_8_8, -7.1_3_2_8, -1.8_4_9_6, 1.9_9_6_1, -8.6_2_5_0, -6.7_2_2_7, -1_2.8_2_8_1, -6.9_4_9_2, -7.0_7_4_2, -7.7_8_5_2, -7.5_8_2_0, -7.9_0_6_2, -6.9_3_7_5, -7.9_8_0_5, -8.3_4_3_8, -8.1_5_6_2, -8.0_4_6_9, -7.6_2_5_0, -7.7_4_2_2, -7.3_3_9_8,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : Optional[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' , device_map='auto' ) lowerCAmelCase__ : List[str] = model(torch.tensor(a ) ) # Expected mean on dim = -1 lowerCAmelCase__ : Union[str, Any] = torch.tensor([[-2.0_6_2_2, -1.2_7_9_4, -1.1_6_3_8, -0.9_7_8_8, -1.4_6_0_3, -1.0_2_3_8, -1.7_8_9_3, -1.4_4_1_1]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : Any = torch.tensor([-8.1_4_0_6, -8.0_5_4_7, 2.7_4_6_1, -1.2_3_4_4, -0.1_4_4_8, -1.8_2_6_2, -1.0_0_2_0, -1.8_1_5_4, -1.6_8_9_5, -1.8_5_1_6, -2.3_5_7_4, -0.9_2_7_7, 3.7_5_9_8, 6.5_7_4_2, -1.2_9_9_8, -0.1_1_7_7, -8.1_4_0_6, -2.9_6_8_8, -2.9_1_9_9, -3.1_6_9_9, -3.5_2_5_4, -2.3_5_5_5, -2.7_9_8_8, -3.4_1_4_1, -2.8_2_6_2, -4.5_1_9_5, -3.3_3_7_9, -3.3_1_6_4, -2.7_8_3_2, -3.0_2_7_3] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : int = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : Optional[int] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' , device_map='auto' ) lowerCAmelCase__ : str = model(torch.tensor(a ) ) # Expected mean on dim = -1 lowerCAmelCase__ : str = torch.tensor([[-0.8_5_6_2, -1.8_5_2_0, -0.7_5_5_1, -0.4_1_6_2, -1.5_1_6_1, -1.2_0_3_8, -2.4_8_2_3, -2.3_2_5_4]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : List[str] = torch.tensor([-2.2_2_2_7, 4.8_8_2_8, 0.9_0_2_3, -0.4_5_7_8, -0.7_8_7_1, -0.1_0_3_3, -0.6_2_2_1, -0.5_7_8_6, -0.7_8_0_3, -1.0_6_7_4, -1.2_9_2_0, -0.1_5_7_0, 0.8_0_0_8, 2.0_7_2_3, -0.9_4_9_7, 0.2_7_7_1, -2.2_2_2_7, -0.7_6_1_2, -1.4_3_4_6, -1.2_0_6_1, -1.6_4_2_6, -0.3_0_0_0, -0.7_1_3_9, -1.1_9_3_4, -1.8_6_9_1, -1.6_9_7_3, -1.5_9_4_7, -1.2_7_0_5, -0.3_5_2_3, -0.5_5_1_3] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' , device_map='auto' ) lowerCAmelCase__ : List[str] = model(torch.tensor(a ) ) lowerCAmelCase__ : int = torch.tensor( [[-4.2_3_2_7, -3.3_3_6_0, -4.6_6_6_5, -4.7_6_3_1, -1.8_1_8_0, -3.4_1_7_0, -1.4_2_1_1, -3.1_8_1_0]] , dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # fmt: off lowerCAmelCase__ : Optional[int] = torch.tensor([-9.4_9_2_2, -3.9_5_5_1, 1.7_9_9_8, -5.6_7_5_8, -5.1_0_5_5, -5.8_9_8_4, -4.8_3_2_0, -6.8_0_8_6, -6.5_3_9_1, -5.6_1_7_2, -5.5_8_2_0, -5.5_3_5_2, 1.7_8_8_1, 3.6_2_8_9, -6.5_1_1_7, -3.4_7_8_5, -9.5_0_0_0, -6.0_3_5_2, -6.8_1_2_5, -6.0_1_9_5, -6.6_8_3_6, -5.4_7_2_7, -6.2_8_1_2, -6.0_3_9_1, -7.3_3_9_8, -7.4_2_9_7, -7.4_8_4_4, -6.5_8_2_0, -5.8_7_8_9, -5.5_3_1_2] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' lowerCAmelCase__ : Tuple = 'Simply put, the theory of relativity states that ' lowerCAmelCase__ : Dict = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) lowerCAmelCase__ : Dict = tokenizer.encode(a , return_tensors='pt' ) lowerCAmelCase__ : str = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' , device_map='sequential' , use_safetensors=a ) # greedy generation outputs lowerCAmelCase__ : Optional[Any] = model.generate(a , max_new_tokens=64 , top_p=a , temperature=1 , do_sample=a ) lowerCAmelCase__ : Tuple = tokenizer.decode(generated_ids[0] , skip_special_tokens=a ) self.assertEqual(a , a )
307
0
from collections import defaultdict from graphs.minimum_spanning_tree_prims import prisms_algorithm as mst def lowerCAmelCase__ ( ) -> Optional[int]: lowerCAmelCase__ : Dict = 9, 14 # noqa: F841 lowerCAmelCase__ : List[Any] = [ [0, 1, 4], [0, 7, 8], [1, 2, 8], [7, 8, 7], [7, 6, 1], [2, 8, 2], [8, 6, 6], [2, 3, 7], [2, 5, 4], [6, 5, 2], [3, 5, 14], [3, 4, 9], [5, 4, 10], [1, 7, 11], ] lowerCAmelCase__ : str = defaultdict(_lowerCAmelCase ) for nodea, nodea, cost in edges: adjancency[nodea].append([nodea, cost] ) adjancency[nodea].append([nodea, cost] ) lowerCAmelCase__ : Tuple = mst(_lowerCAmelCase ) lowerCAmelCase__ : Tuple = [ [7, 6, 1], [2, 8, 2], [6, 5, 2], [0, 1, 4], [2, 5, 4], [2, 3, 7], [0, 7, 8], [3, 4, 9], ] for answer in expected: lowerCAmelCase__ : List[Any] = tuple(answer[:2] ) lowerCAmelCase__ : Any = tuple(edge[::-1] ) assert edge in result or reverse in result
371
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """microsoft/unispeech-large-1500h-cv""": ( """https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json""" ), # See all UniSpeech models at https://huggingface.co/models?filter=unispeech } class A__ ( __magic_name__ ): lowercase = 'unispeech' def __init__( self : Any , a : List[Any]=32 , a : List[Any]=768 , a : Any=12 , a : List[str]=12 , a : List[Any]=3_072 , a : Any="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : List[str]=0.1 , a : Union[str, Any]=0.0 , a : str=0.0 , a : int=0.1 , a : List[str]=0.1 , a : List[Any]=0.0_2 , a : Optional[int]=1E-5 , a : Optional[int]="group" , a : Optional[Any]="gelu" , a : List[Any]=(512, 512, 512, 512, 512, 512, 512) , a : Optional[Any]=(5, 2, 2, 2, 2, 2, 2) , a : List[str]=(10, 3, 3, 3, 3, 2, 2) , a : Union[str, Any]=False , a : Union[str, Any]=128 , a : Tuple=16 , a : Dict=False , a : str=True , a : str=0.0_5 , a : Union[str, Any]=10 , a : Tuple=2 , a : int=0.0 , a : Optional[Any]=10 , a : List[str]=0 , a : str=320 , a : List[str]=2 , a : Optional[Any]=0.1 , a : Any=100 , a : Dict=256 , a : Any=256 , a : Dict=0.1 , a : List[Any]="mean" , a : Dict=False , a : str=False , a : Optional[int]=256 , a : Any=80 , a : List[Any]=0 , a : Optional[int]=1 , a : int=2 , a : List[Any]=0.5 , **a : int , ): '''simple docstring''' super().__init__(**a , pad_token_id=a , bos_token_id=a , eos_token_id=a ) lowerCAmelCase__ : List[str] = hidden_size lowerCAmelCase__ : List[str] = feat_extract_norm lowerCAmelCase__ : Optional[Any] = feat_extract_activation lowerCAmelCase__ : str = list(a ) lowerCAmelCase__ : List[str] = list(a ) lowerCAmelCase__ : Tuple = list(a ) lowerCAmelCase__ : Dict = conv_bias lowerCAmelCase__ : Optional[int] = num_conv_pos_embeddings lowerCAmelCase__ : Any = num_conv_pos_embedding_groups lowerCAmelCase__ : str = len(self.conv_dim ) lowerCAmelCase__ : Any = num_hidden_layers lowerCAmelCase__ : Dict = intermediate_size lowerCAmelCase__ : Dict = hidden_act lowerCAmelCase__ : Union[str, Any] = num_attention_heads lowerCAmelCase__ : Union[str, Any] = hidden_dropout lowerCAmelCase__ : Tuple = attention_dropout lowerCAmelCase__ : str = activation_dropout lowerCAmelCase__ : Any = feat_proj_dropout lowerCAmelCase__ : List[Any] = final_dropout lowerCAmelCase__ : Tuple = layerdrop lowerCAmelCase__ : Any = layer_norm_eps lowerCAmelCase__ : Dict = initializer_range lowerCAmelCase__ : Optional[Any] = num_ctc_classes lowerCAmelCase__ : Tuple = vocab_size lowerCAmelCase__ : Dict = do_stable_layer_norm lowerCAmelCase__ : List[Any] = use_weighted_layer_sum lowerCAmelCase__ : Any = classifier_proj_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)`, but is `len(config.conv_dim) =' f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCAmelCase__ : Union[str, Any] = apply_spec_augment lowerCAmelCase__ : Any = mask_time_prob lowerCAmelCase__ : Dict = mask_time_length lowerCAmelCase__ : Tuple = mask_time_min_masks lowerCAmelCase__ : Optional[int] = mask_feature_prob lowerCAmelCase__ : Optional[Any] = mask_feature_length lowerCAmelCase__ : int = mask_feature_min_masks # parameters for pretraining with codevector quantized representations lowerCAmelCase__ : int = num_codevectors_per_group lowerCAmelCase__ : Any = num_codevector_groups lowerCAmelCase__ : Any = contrastive_logits_temperature lowerCAmelCase__ : int = feat_quantizer_dropout lowerCAmelCase__ : List[Any] = num_negatives lowerCAmelCase__ : List[str] = codevector_dim lowerCAmelCase__ : Optional[int] = proj_codevector_dim lowerCAmelCase__ : Dict = diversity_loss_weight # ctc loss lowerCAmelCase__ : Any = ctc_loss_reduction lowerCAmelCase__ : Any = ctc_zero_infinity # pretraining loss lowerCAmelCase__ : Union[str, Any] = replace_prob @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = 1_000 ) -> int: lowerCAmelCase__ : List[Any] = 1, 1 lowerCAmelCase__ : Union[str, Any] = 2 while True: lowerCAmelCase__ : Optional[int] = 0 lowerCAmelCase__ : str = fa + fa lowerCAmelCase__ : Optional[int] = fa, f index += 1 for _ in str(__lowerCAmelCase ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
350
import torch from torch import nn class A__ ( nn.Module ): def __init__( self : Optional[int] , a : Union[str, Any] , a : str , a : str , a : List[Any] , a : List[Any]=1 , a : Tuple=False ): '''simple docstring''' super().__init__() lowerCAmelCase__ : Dict = n_token lowerCAmelCase__ : Any = d_embed lowerCAmelCase__ : str = d_proj lowerCAmelCase__ : int = cutoffs + [n_token] lowerCAmelCase__ : Union[str, Any] = [0] + self.cutoffs lowerCAmelCase__ : str = div_val lowerCAmelCase__ : Tuple = self.cutoffs[0] lowerCAmelCase__ : Dict = len(self.cutoffs ) - 1 lowerCAmelCase__ : Any = self.shortlist_size + self.n_clusters if self.n_clusters > 0: lowerCAmelCase__ : int = nn.Parameter(torch.zeros(self.n_clusters , self.d_embed ) ) lowerCAmelCase__ : Optional[Any] = nn.Parameter(torch.zeros(self.n_clusters ) ) lowerCAmelCase__ : Optional[int] = nn.ModuleList() lowerCAmelCase__ : Tuple = nn.ParameterList() if div_val == 1: for i in range(len(self.cutoffs ) ): if d_proj != d_embed: self.out_projs.append(nn.Parameter(torch.FloatTensor(a , a ) ) ) else: self.out_projs.append(a ) self.out_layers.append(nn.Linear(a , a ) ) else: for i in range(len(self.cutoffs ) ): lowerCAmelCase__ , lowerCAmelCase__ : Any = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : Optional[Any] = d_embed // (div_val**i) self.out_projs.append(nn.Parameter(torch.FloatTensor(a , a ) ) ) self.out_layers.append(nn.Linear(a , r_idx - l_idx ) ) lowerCAmelCase__ : Tuple = keep_order def _lowerCamelCase ( self : Optional[int] , a : List[str] , a : int , a : List[str] , a : str ): '''simple docstring''' if proj is None: lowerCAmelCase__ : Tuple = nn.functional.linear(a , a , bias=a ) else: # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1: lowerCAmelCase__ : int = nn.functional.linear(a , proj.t().contiguous() ) lowerCAmelCase__ : Tuple = nn.functional.linear(a , a , bias=a ) # else: # logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t())) # if bias is not None: # logit = logit + bias return logit def _lowerCamelCase ( self : List[str] , a : List[Any] , a : Optional[int]=None , a : Tuple=False ): '''simple docstring''' if labels is not None: # Shift so that tokens < n predict n lowerCAmelCase__ : str = hidden[..., :-1, :].contiguous() lowerCAmelCase__ : Optional[Any] = labels[..., 1:].contiguous() lowerCAmelCase__ : List[Any] = hidden.view(-1 , hidden.size(-1 ) ) lowerCAmelCase__ : Tuple = labels.view(-1 ) if hidden.size(0 ) != labels.size(0 ): raise RuntimeError('Input and labels should have the same size in the batch dimension.' ) else: lowerCAmelCase__ : Optional[Any] = hidden.view(-1 , hidden.size(-1 ) ) if self.n_clusters == 0: lowerCAmelCase__ : Optional[Any] = self._compute_logit(a , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) if labels is not None: lowerCAmelCase__ : str = labels != -100 lowerCAmelCase__ : int = torch.zeros_like(a , dtype=hidden.dtype , device=hidden.device ) lowerCAmelCase__ : List[str] = ( -nn.functional.log_softmax(a , dim=-1 )[mask].gather(1 , labels[mask].unsqueeze(1 ) ).squeeze(1 ) ) else: lowerCAmelCase__ : Optional[Any] = nn.functional.log_softmax(a , dim=-1 ) else: # construct weights and biases lowerCAmelCase__ , lowerCAmelCase__ : int = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : Any = self.out_layers[0].weight[l_idx:r_idx] lowerCAmelCase__ : Any = self.out_layers[0].bias[l_idx:r_idx] else: lowerCAmelCase__ : Optional[Any] = self.out_layers[i].weight lowerCAmelCase__ : Optional[int] = self.out_layers[i].bias if i == 0: lowerCAmelCase__ : Dict = torch.cat([weight_i, self.cluster_weight] , dim=0 ) lowerCAmelCase__ : Union[str, Any] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(a ) biases.append(a ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = weights[0], biases[0], self.out_projs[0] lowerCAmelCase__ : List[Any] = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : Union[str, Any] = nn.functional.log_softmax(a , dim=1 ) if labels is None: lowerCAmelCase__ : Tuple = hidden.new_empty((head_logit.size(0 ), self.n_token) ) else: lowerCAmelCase__ : Dict = torch.zeros_like(a , dtype=hidden.dtype , device=hidden.device ) lowerCAmelCase__ : Tuple = 0 lowerCAmelCase__ : Union[str, Any] = [0] + self.cutoffs for i in range(len(a ) - 1 ): lowerCAmelCase__ , lowerCAmelCase__ : Tuple = cutoff_values[i], cutoff_values[i + 1] if labels is not None: lowerCAmelCase__ : Tuple = (labels >= l_idx) & (labels < r_idx) lowerCAmelCase__ : int = mask_i.nonzero().squeeze() if indices_i.numel() == 0: continue lowerCAmelCase__ : Tuple = labels.index_select(0 , a ) - l_idx lowerCAmelCase__ : Any = head_logprob.index_select(0 , a ) lowerCAmelCase__ : Optional[int] = hidden.index_select(0 , a ) else: lowerCAmelCase__ : Any = hidden if i == 0: if labels is not None: lowerCAmelCase__ : Union[str, Any] = head_logprob_i.gather(1 , target_i[:, None] ).squeeze(1 ) else: lowerCAmelCase__ : List[str] = head_logprob[:, : self.cutoffs[0]] else: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Any = weights[i], biases[i], self.out_projs[i] lowerCAmelCase__ : Union[str, Any] = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : Optional[int] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : List[Any] = self.cutoffs[0] + i - 1 # No probability for the head cluster if labels is not None: lowerCAmelCase__ : List[str] = head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather( 1 , target_i[:, None] ).squeeze(1 ) else: lowerCAmelCase__ : Tuple = head_logprob[:, cluster_prob_idx, None] + tail_logprob_i lowerCAmelCase__ : Union[str, Any] = logprob_i if labels is not None: if (hasattr(self , 'keep_order' ) and self.keep_order) or keep_order: out.index_copy_(0 , a , -logprob_i ) else: out[offset : offset + logprob_i.size(0 )].copy_(-logprob_i ) offset += logprob_i.size(0 ) return out def _lowerCamelCase ( self : List[Any] , a : Any ): '''simple docstring''' if self.n_clusters == 0: lowerCAmelCase__ : Union[str, Any] = self._compute_logit(a , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) return nn.functional.log_softmax(a , dim=-1 ) else: # construct weights and biases lowerCAmelCase__ , lowerCAmelCase__ : str = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : str = self.out_layers[0].weight[l_idx:r_idx] lowerCAmelCase__ : Dict = self.out_layers[0].bias[l_idx:r_idx] else: lowerCAmelCase__ : int = self.out_layers[i].weight lowerCAmelCase__ : int = self.out_layers[i].bias if i == 0: lowerCAmelCase__ : Optional[int] = torch.cat([weight_i, self.cluster_weight] , dim=0 ) lowerCAmelCase__ : Union[str, Any] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(a ) biases.append(a ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : str = weights[0], biases[0], self.out_projs[0] lowerCAmelCase__ : Dict = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : List[Any] = hidden.new_empty((head_logit.size(0 ), self.n_token) ) lowerCAmelCase__ : Optional[Any] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : List[Any] = [0] + self.cutoffs for i in range(len(a ) - 1 ): lowerCAmelCase__ , lowerCAmelCase__ : str = cutoff_values[i], cutoff_values[i + 1] if i == 0: lowerCAmelCase__ : Union[str, Any] = head_logprob[:, : self.cutoffs[0]] else: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = weights[i], biases[i], self.out_projs[i] lowerCAmelCase__ : Dict = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : List[str] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : Dict = head_logprob[:, -i] + tail_logprob_i lowerCAmelCase__ : List[str] = logprob_i return out
307
0
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ConditionalDetrImageProcessor class A__ ( unittest.TestCase ): def __init__( self : Any , a : Union[str, Any] , a : List[str]=7 , a : List[Any]=3 , a : int=30 , a : Any=400 , a : List[str]=True , a : Any=None , a : Union[str, Any]=True , a : Union[str, Any]=[0.5, 0.5, 0.5] , a : List[Any]=[0.5, 0.5, 0.5] , a : str=True , a : Optional[int]=1 / 255 , a : Dict=True , ): '''simple docstring''' lowerCAmelCase__ : Dict = size if size is not None else {'shortest_edge': 18, 'longest_edge': 1_333} lowerCAmelCase__ : str = parent lowerCAmelCase__ : Optional[int] = batch_size lowerCAmelCase__ : int = num_channels lowerCAmelCase__ : int = min_resolution lowerCAmelCase__ : Dict = max_resolution lowerCAmelCase__ : Dict = do_resize lowerCAmelCase__ : Any = size lowerCAmelCase__ : Any = do_normalize lowerCAmelCase__ : int = image_mean lowerCAmelCase__ : Tuple = image_std lowerCAmelCase__ : Optional[int] = do_rescale lowerCAmelCase__ : str = rescale_factor lowerCAmelCase__ : Optional[int] = do_pad def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def _lowerCamelCase ( self : Optional[Any] , a : List[Any] , a : Union[str, Any]=False ): '''simple docstring''' if not batched: lowerCAmelCase__ : Dict = image_inputs[0] if isinstance(a , Image.Image ): lowerCAmelCase__ : Tuple = image.size else: lowerCAmelCase__ : Union[str, Any] = image.shape[1], image.shape[2] if w < h: lowerCAmelCase__ : str = int(self.size['shortest_edge'] * h / w ) lowerCAmelCase__ : str = self.size['shortest_edge'] elif w > h: lowerCAmelCase__ : int = self.size['shortest_edge'] lowerCAmelCase__ : Optional[int] = int(self.size['shortest_edge'] * w / h ) else: lowerCAmelCase__ : List[Any] = self.size['shortest_edge'] lowerCAmelCase__ : int = self.size['shortest_edge'] else: lowerCAmelCase__ : List[Any] = [] for image in image_inputs: lowerCAmelCase__ : Dict = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) lowerCAmelCase__ : List[str] = max(a , key=lambda a : item[0] )[0] lowerCAmelCase__ : List[Any] = max(a , key=lambda a : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class A__ ( __UpperCamelCase , unittest.TestCase ): lowercase = ConditionalDetrImageProcessor if is_vision_available() else None def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Any = ConditionalDetrImageProcessingTester(self ) @property def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a , 'image_mean' ) ) self.assertTrue(hasattr(a , 'image_std' ) ) self.assertTrue(hasattr(a , 'do_normalize' ) ) self.assertTrue(hasattr(a , 'do_resize' ) ) self.assertTrue(hasattr(a , 'size' ) ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'shortest_edge': 18, 'longest_edge': 1_333} ) self.assertEqual(image_processor.do_pad , a ) lowerCAmelCase__ : Dict = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=a ) self.assertEqual(image_processor.size , {'shortest_edge': 42, 'longest_edge': 84} ) self.assertEqual(image_processor.do_pad , a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowerCAmelCase__ : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=a ) for image in image_inputs: self.assertIsInstance(a , Image.Image ) # Test not batched input lowerCAmelCase__ : Optional[int] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values lowerCAmelCase__ : Union[str, Any] = self.image_processor_tester.get_expected_values(a ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCAmelCase__ : Optional[int] = self.image_processor_tester.get_expected_values(a , batched=a ) lowerCAmelCase__ : Optional[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, expected_height, expected_width, ) , ) def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCAmelCase__ : 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 lowerCAmelCase__ : Dict = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values lowerCAmelCase__ : Tuple = self.image_processor_tester.get_expected_values(a ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCAmelCase__ : List[Any] = image_processing(a , return_tensors='pt' ).pixel_values lowerCAmelCase__ : Dict = self.image_processor_tester.get_expected_values(a , batched=a ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCAmelCase__ : 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 lowerCAmelCase__ : Optional[int] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values lowerCAmelCase__ : int = self.image_processor_tester.get_expected_values(a ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCAmelCase__ : List[str] = image_processing(a , return_tensors='pt' ).pixel_values lowerCAmelCase__ : Any = self.image_processor_tester.get_expected_values(a , batched=a ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : int = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) with open('./tests/fixtures/tests_samples/COCO/coco_annotations.txt' , 'r' ) as f: lowerCAmelCase__ : int = json.loads(f.read() ) lowerCAmelCase__ : Any = {'image_id': 39_769, 'annotations': target} # encode them lowerCAmelCase__ : Any = ConditionalDetrImageProcessor.from_pretrained('microsoft/conditional-detr-resnet-50' ) lowerCAmelCase__ : Union[str, Any] = image_processing(images=a , annotations=a , return_tensors='pt' ) # verify pixel values lowerCAmelCase__ : List[str] = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding['pixel_values'].shape , a ) lowerCAmelCase__ : Optional[Any] = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3] , a , atol=1E-4 ) ) # verify area lowerCAmelCase__ : Optional[int] = torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438] ) self.assertTrue(torch.allclose(encoding['labels'][0]['area'] , a ) ) # verify boxes lowerCAmelCase__ : str = torch.Size([6, 4] ) self.assertEqual(encoding['labels'][0]['boxes'].shape , a ) lowerCAmelCase__ : Optional[int] = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0] , a , atol=1E-3 ) ) # verify image_id lowerCAmelCase__ : Any = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'] , a ) ) # verify is_crowd lowerCAmelCase__ : List[Any] = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'] , a ) ) # verify class_labels lowerCAmelCase__ : Dict = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'] , a ) ) # verify orig_size lowerCAmelCase__ : Union[str, Any] = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'] , a ) ) # verify size lowerCAmelCase__ : Union[str, Any] = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding['labels'][0]['size'] , a ) ) @slow def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : List[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) with open('./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt' , 'r' ) as f: lowerCAmelCase__ : Dict = json.loads(f.read() ) lowerCAmelCase__ : Optional[int] = {'file_name': '000000039769.png', 'image_id': 39_769, 'segments_info': target} lowerCAmelCase__ : Tuple = pathlib.Path('./tests/fixtures/tests_samples/COCO/coco_panoptic' ) # encode them lowerCAmelCase__ : Union[str, Any] = ConditionalDetrImageProcessor(format='coco_panoptic' ) lowerCAmelCase__ : Optional[Any] = image_processing(images=a , annotations=a , masks_path=a , return_tensors='pt' ) # verify pixel values lowerCAmelCase__ : Dict = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding['pixel_values'].shape , a ) lowerCAmelCase__ : Optional[Any] = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3] , a , atol=1E-4 ) ) # verify area lowerCAmelCase__ : str = torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147] ) self.assertTrue(torch.allclose(encoding['labels'][0]['area'] , a ) ) # verify boxes lowerCAmelCase__ : Any = torch.Size([6, 4] ) self.assertEqual(encoding['labels'][0]['boxes'].shape , a ) lowerCAmelCase__ : Optional[Any] = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0] , a , atol=1E-3 ) ) # verify image_id lowerCAmelCase__ : Tuple = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'] , a ) ) # verify is_crowd lowerCAmelCase__ : int = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'] , a ) ) # verify class_labels lowerCAmelCase__ : Dict = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'] , a ) ) # verify masks lowerCAmelCase__ : List[Any] = 822_873 self.assertEqual(encoding['labels'][0]['masks'].sum().item() , a ) # verify orig_size lowerCAmelCase__ : Tuple = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'] , a ) ) # verify size lowerCAmelCase__ : Union[str, Any] = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding['labels'][0]['size'] , a ) )
351
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate lowerCamelCase__ = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow("""""", """|""", """|"""), datarow=DataRow("""""", """|""", """|"""), padding=1, with_header_hide=None, ) lowerCamelCase__ = [] lowerCamelCase__ = [] lowerCamelCase__ = {"""type""": """section""", """text""": {"""type""": """plain_text""", """text""": """No failed tests! 🤗""", """emoji""": True}} lowerCamelCase__ = [ { """type""": """header""", """text""": { """type""": """plain_text""", """text""": F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", """emoji""": True, }, } ] lowerCamelCase__ = 0 for log in Path().glob("""*.log"""): lowerCamelCase__ = 0 with open(log, """r""") as f: for line in f: lowerCamelCase__ = json.loads(line) if line.get("""nodeid""", """""") != "": lowerCamelCase__ = line["""nodeid"""] if line.get("""duration""", None) is not None: lowerCamelCase__ = F"""{line["duration"]:.4f}""" if line.get("""outcome""", """""") == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split("""_""")[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) lowerCamelCase__ = [] log.unlink() lowerCamelCase__ = """""" lowerCamelCase__ = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" lowerCamelCase__ = [] lowerCamelCase__ = {} for test in failed_tests: lowerCamelCase__ = test[0].split("""::""") lowerCamelCase__ = data[0].split("""/""")[-1] if data[0] not in filesafailed: lowerCamelCase__ = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) lowerCamelCase__ = [test[0] for test in failed_table] lowerCamelCase__ = list(set(files)) # Count number of instances in failed_tests lowerCamelCase__ = [] for file in individual_files: table.append([file, len(filesafailed[file])]) lowerCamelCase__ = tabulate( table, headers=["""Test Location""", """Num Failed"""], tablefmt=hf_table_format, stralign="""right""", ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 3000: lowerCamelCase__ = """Too many failed tests, please see the full report in the Action results.""" lowerCamelCase__ = len(err) + 10 lowerCamelCase__ = message[: 3000 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: lowerCamelCase__ = """No failed tests! 🤗""" print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get("""TEST_TYPE""", """""") != "": from slack_sdk import WebClient lowerCamelCase__ = WebClient(token=os.environ["""SLACK_API_TOKEN"""]) if message != "No failed tests! 🤗": lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": message, }, } payload.append(md_report) lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": """*For more details:*""", }, """accessory""": { """type""": """button""", """text""": { """type""": """plain_text""", """text""": """Check Action results""", """emoji""": True, }, """url""": F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) lowerCamelCase__ = { """type""": """context""", """elements""": [ { """type""": """plain_text""", """text""": F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) lowerCamelCase__ = client.chat_postMessage(channel="""#accelerate-ci-daily""", text=message, blocks=payload) lowerCamelCase__ = response.data["""ts"""] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name lowerCamelCase__ = """""" for i, row in enumerate(test_failures): if row[0] != test_class: lowerCamelCase__ = row[0] else: lowerCamelCase__ = """""" lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel="""#accelerate-ci-daily""", thread_ts=ts, blocks=[payload], )
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = " " ) -> list: lowerCAmelCase__ : List[str] = [] lowerCAmelCase__ : Optional[int] = 0 for index, char in enumerate(SCREAMING_SNAKE_CASE_ ): if char == separator: split_words.append(string[last_index:index] ) lowerCAmelCase__ : int = index + 1 elif index + 1 == len(SCREAMING_SNAKE_CASE_ ): split_words.append(string[last_index : index + 1] ) return split_words if __name__ == "__main__": from doctest import testmod testmod()
352
import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs lowerCamelCase__ = imread(r"""digital_image_processing/image_data/lena_small.jpg""") lowerCamelCase__ = cvtColor(img, COLOR_BGR2GRAY) def lowerCAmelCase__ ( ) -> Dict: lowerCAmelCase__ : List[Any] = cn.convert_to_negative(SCREAMING_SNAKE_CASE_ ) # assert negative_img array for at least one True assert negative_img.any() def lowerCAmelCase__ ( ) -> Optional[Any]: with Image.open('digital_image_processing/image_data/lena_small.jpg' ) as img: # Work around assertion for response assert str(cc.change_contrast(SCREAMING_SNAKE_CASE_ , 110 ) ).startswith( '<PIL.Image.Image image mode=RGB size=100x100 at' ) def lowerCAmelCase__ ( ) -> Tuple: lowerCAmelCase__ : str = canny.gen_gaussian_kernel(9 , sigma=1.4 ) # Assert ambiguous array assert resp.all() def lowerCAmelCase__ ( ) -> Tuple: lowerCAmelCase__ : Tuple = imread('digital_image_processing/image_data/lena_small.jpg' , 0 ) # assert ambiguous array for all == True assert canny_img.all() lowerCAmelCase__ : Optional[Any] = canny.canny(SCREAMING_SNAKE_CASE_ ) # assert canny array for at least one True assert canny_array.any() def lowerCAmelCase__ ( ) -> Optional[int]: assert gg.gaussian_filter(SCREAMING_SNAKE_CASE_ , 5 , sigma=0.9 ).all() def lowerCAmelCase__ ( ) -> Dict: # laplace diagonals lowerCAmelCase__ : Union[str, Any] = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]] ) lowerCAmelCase__ : int = conv.img_convolve(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).astype(SCREAMING_SNAKE_CASE_ ) assert res.any() def lowerCAmelCase__ ( ) -> List[str]: assert med.median_filter(SCREAMING_SNAKE_CASE_ , 3 ).any() def lowerCAmelCase__ ( ) -> Any: lowerCAmelCase__ , lowerCAmelCase__ : str = sob.sobel_filter(SCREAMING_SNAKE_CASE_ ) assert grad.any() and theta.any() def lowerCAmelCase__ ( ) -> Any: lowerCAmelCase__ : int = sp.make_sepia(SCREAMING_SNAKE_CASE_ , 20 ) assert sepia.all() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = "digital_image_processing/image_data/lena_small.jpg" ) -> Optional[Any]: lowerCAmelCase__ : List[Any] = bs.Burkes(imread(SCREAMING_SNAKE_CASE_ , 1 ) , 120 ) burkes.process() assert burkes.output_img.any() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = "digital_image_processing/image_data/lena_small.jpg" , ) -> Any: lowerCAmelCase__ : Dict = rs.NearestNeighbour(imread(SCREAMING_SNAKE_CASE_ , 1 ) , 400 , 200 ) nn.process() assert nn.output.any() def lowerCAmelCase__ ( ) -> int: lowerCAmelCase__ : int = 'digital_image_processing/image_data/lena.jpg' # Reading the image and converting it to grayscale. lowerCAmelCase__ : List[str] = imread(SCREAMING_SNAKE_CASE_ , 0 ) # Test for get_neighbors_pixel function() return not None lowerCAmelCase__ : str = 0 lowerCAmelCase__ : str = 0 lowerCAmelCase__ : List[str] = image[x_coordinate][y_coordinate] lowerCAmelCase__ : Dict = lbp.get_neighbors_pixel( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image lowerCAmelCase__ : List[str] = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 , image.shape[0] ): for j in range(0 , image.shape[1] ): lowerCAmelCase__ : Dict = lbp.local_binary_value(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert lbp_image.any()
307
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """google/bigbird-roberta-base""": """https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json""", """google/bigbird-roberta-large""": """https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json""", """google/bigbird-base-trivia-itc""": """https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json""", # See all BigBird models at https://huggingface.co/models?filter=big_bird } class A__ ( snake_case_ ): lowercase = 'big_bird' def __init__( self : Any , a : Dict=50_358 , a : Tuple=768 , a : Any=12 , a : Tuple=12 , a : List[Any]=3_072 , a : Any="gelu_new" , a : Tuple=0.1 , a : Tuple=0.1 , a : Optional[int]=4_096 , a : Union[str, Any]=2 , a : Optional[Any]=0.0_2 , a : List[Any]=1E-12 , a : Dict=True , a : List[Any]=0 , a : List[str]=1 , a : Optional[Any]=2 , a : Dict=66 , a : Tuple="block_sparse" , a : Dict=True , a : Union[str, Any]=False , a : List[str]=64 , a : Optional[int]=3 , a : Optional[Any]=None , **a : int , ): '''simple docstring''' super().__init__( pad_token_id=a , bos_token_id=a , eos_token_id=a , sep_token_id=a , **a , ) lowerCAmelCase__ : Dict = vocab_size lowerCAmelCase__ : List[str] = max_position_embeddings lowerCAmelCase__ : str = hidden_size lowerCAmelCase__ : Optional[Any] = num_hidden_layers lowerCAmelCase__ : Dict = num_attention_heads lowerCAmelCase__ : Optional[int] = intermediate_size lowerCAmelCase__ : Union[str, Any] = hidden_act lowerCAmelCase__ : Dict = hidden_dropout_prob lowerCAmelCase__ : List[str] = attention_probs_dropout_prob lowerCAmelCase__ : List[Any] = initializer_range lowerCAmelCase__ : Dict = type_vocab_size lowerCAmelCase__ : Optional[Any] = layer_norm_eps lowerCAmelCase__ : List[Any] = use_cache lowerCAmelCase__ : int = rescale_embeddings lowerCAmelCase__ : List[Any] = attention_type lowerCAmelCase__ : Any = use_bias lowerCAmelCase__ : Union[str, Any] = block_size lowerCAmelCase__ : Optional[int] = num_random_blocks lowerCAmelCase__ : Optional[int] = classifier_dropout class A__ ( snake_case_ ): @property def _lowerCamelCase ( self : Any ): '''simple docstring''' if self.task == "multiple-choice": lowerCAmelCase__ : List[Any] = {0: 'batch', 1: 'choice', 2: 'sequence'} else: lowerCAmelCase__ : List[Any] = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
353
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCamelCase__ = { """configuration_blip""": [ """BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BlipConfig""", """BlipTextConfig""", """BlipVisionConfig""", ], """processing_blip""": ["""BlipProcessor"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""BlipImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """BlipModel""", """BlipPreTrainedModel""", """BlipForConditionalGeneration""", """BlipForQuestionAnswering""", """BlipVisionModel""", """BlipTextModel""", """BlipForImageTextRetrieval""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFBlipModel""", """TFBlipPreTrainedModel""", """TFBlipForConditionalGeneration""", """TFBlipForQuestionAnswering""", """TFBlipVisionModel""", """TFBlipTextModel""", """TFBlipForImageTextRetrieval""", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
from math import sqrt def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = 1_000_000 ) -> Optional[int]: lowerCAmelCase__ : Dict = 0 lowerCAmelCase__ : Union[str, Any] = 0 lowerCAmelCase__ : Dict = 42 while num_cuboids <= limit: max_cuboid_size += 1 for sum_shortest_sides in range(2 , 2 * max_cuboid_size + 1 ): if sqrt(sum_shortest_sides**2 + max_cuboid_size**2 ).is_integer(): num_cuboids += ( min(_lowerCAmelCase , sum_shortest_sides // 2 ) - max(1 , sum_shortest_sides - max_cuboid_size ) + 1 ) return max_cuboid_size if __name__ == "__main__": print(F"""{solution() = }""")
354
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=0.999 , SCREAMING_SNAKE_CASE_="cosine" , ) -> Union[str, Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(SCREAMING_SNAKE_CASE_ ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(SCREAMING_SNAKE_CASE_ ): return math.exp(t * -12.0 ) else: raise ValueError(F'''Unsupported alpha_tranform_type: {alpha_transform_type}''' ) lowerCAmelCase__ : Tuple = [] for i in range(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : List[Any] = i / num_diffusion_timesteps lowerCAmelCase__ : str = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) return torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.floataa ) class A__ ( __magic_name__ , __magic_name__ ): lowercase = [e.name for e in KarrasDiffusionSchedulers] lowercase = 2 @register_to_config def __init__( self : Union[str, Any] , a : int = 1_000 , a : float = 0.0_0_0_8_5 , a : float = 0.0_1_2 , a : str = "linear" , a : Optional[Union[np.ndarray, List[float]]] = None , a : str = "epsilon" , a : Optional[bool] = False , a : Optional[bool] = False , a : float = 1.0 , a : str = "linspace" , a : int = 0 , ): '''simple docstring''' if trained_betas is not None: lowerCAmelCase__ : List[str] = torch.tensor(a , dtype=torch.floataa ) elif beta_schedule == "linear": lowerCAmelCase__ : List[str] = torch.linspace(a , a , a , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowerCAmelCase__ : Union[str, Any] = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , a , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowerCAmelCase__ : int = betas_for_alpha_bar(a , alpha_transform_type='cosine' ) elif beta_schedule == "exp": lowerCAmelCase__ : List[str] = betas_for_alpha_bar(a , alpha_transform_type='exp' ) else: raise NotImplementedError(f'''{beta_schedule} does is not implemented for {self.__class__}''' ) lowerCAmelCase__ : int = 1.0 - self.betas lowerCAmelCase__ : Tuple = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(a , a , a ) lowerCAmelCase__ : Optional[Any] = use_karras_sigmas def _lowerCamelCase ( self : str , a : List[Any] , a : str=None ): '''simple docstring''' if schedule_timesteps is None: lowerCAmelCase__ : List[str] = self.timesteps lowerCAmelCase__ : int = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: lowerCAmelCase__ : List[str] = 1 if len(a ) > 1 else 0 else: lowerCAmelCase__ : Tuple = timestep.cpu().item() if torch.is_tensor(a ) else timestep lowerCAmelCase__ : Tuple = self._index_counter[timestep_int] return indices[pos].item() @property def _lowerCamelCase ( self : Dict ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _lowerCamelCase ( self : Tuple , a : torch.FloatTensor , a : Union[float, torch.FloatTensor] , ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.index_for_timestep(a ) lowerCAmelCase__ : Any = self.sigmas[step_index] lowerCAmelCase__ : Optional[Any] = sample / ((sigma**2 + 1) ** 0.5) return sample def _lowerCamelCase ( self : List[str] , a : int , a : Union[str, torch.device] = None , a : Optional[int] = None , ): '''simple docstring''' lowerCAmelCase__ : Any = num_inference_steps lowerCAmelCase__ : Union[str, Any] = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": lowerCAmelCase__ : Union[str, Any] = np.linspace(0 , num_train_timesteps - 1 , a , dtype=a )[::-1].copy() elif self.config.timestep_spacing == "leading": lowerCAmelCase__ : List[Any] = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : Dict = (np.arange(0 , a ) * step_ratio).round()[::-1].copy().astype(a ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": lowerCAmelCase__ : Tuple = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : int = (np.arange(a , 0 , -step_ratio )).round().copy().astype(a ) timesteps -= 1 else: raise ValueError( f'''{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.''' ) lowerCAmelCase__ : str = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) lowerCAmelCase__ : List[Any] = np.log(a ) lowerCAmelCase__ : Optional[int] = np.interp(a , np.arange(0 , len(a ) ) , a ) if self.config.use_karras_sigmas: lowerCAmelCase__ : str = self._convert_to_karras(in_sigmas=a , num_inference_steps=self.num_inference_steps ) lowerCAmelCase__ : Union[str, Any] = np.array([self._sigma_to_t(a , a ) for sigma in sigmas] ) lowerCAmelCase__ : Tuple = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) lowerCAmelCase__ : Dict = torch.from_numpy(a ).to(device=a ) lowerCAmelCase__ : Union[str, Any] = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) lowerCAmelCase__ : Tuple = torch.from_numpy(a ) lowerCAmelCase__ : List[str] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(a ).startswith('mps' ): # mps does not support float64 lowerCAmelCase__ : Optional[Any] = timesteps.to(a , dtype=torch.floataa ) else: lowerCAmelCase__ : Any = timesteps.to(device=a ) # empty dt and derivative lowerCAmelCase__ : str = None lowerCAmelCase__ : Optional[int] = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter lowerCAmelCase__ : Optional[Any] = defaultdict(a ) def _lowerCamelCase ( self : Any , a : Dict , a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.log(a ) # get distribution lowerCAmelCase__ : Tuple = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range lowerCAmelCase__ : Optional[int] = np.cumsum((dists >= 0) , axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) lowerCAmelCase__ : List[str] = low_idx + 1 lowerCAmelCase__ : List[str] = log_sigmas[low_idx] lowerCAmelCase__ : Any = log_sigmas[high_idx] # interpolate sigmas lowerCAmelCase__ : Union[str, Any] = (low - log_sigma) / (low - high) lowerCAmelCase__ : List[Any] = np.clip(a , 0 , 1 ) # transform interpolation to time range lowerCAmelCase__ : List[Any] = (1 - w) * low_idx + w * high_idx lowerCAmelCase__ : Any = t.reshape(sigma.shape ) return t def _lowerCamelCase ( self : Tuple , a : torch.FloatTensor , a : Any ): '''simple docstring''' lowerCAmelCase__ : float = in_sigmas[-1].item() lowerCAmelCase__ : float = in_sigmas[0].item() lowerCAmelCase__ : Tuple = 7.0 # 7.0 is the value used in the paper lowerCAmelCase__ : Tuple = np.linspace(0 , 1 , a ) lowerCAmelCase__ : Any = sigma_min ** (1 / rho) lowerCAmelCase__ : Optional[Any] = sigma_max ** (1 / rho) lowerCAmelCase__ : Tuple = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def _lowerCamelCase ( self : Any ): '''simple docstring''' return self.dt is None def _lowerCamelCase ( self : List[str] , a : Union[torch.FloatTensor, np.ndarray] , a : Union[float, torch.FloatTensor] , a : Union[torch.FloatTensor, np.ndarray] , a : bool = True , ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.index_for_timestep(a ) # advance index counter by 1 lowerCAmelCase__ : Tuple = timestep.cpu().item() if torch.is_tensor(a ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: lowerCAmelCase__ : Union[str, Any] = self.sigmas[step_index] lowerCAmelCase__ : Union[str, Any] = self.sigmas[step_index + 1] else: # 2nd order / Heun's method lowerCAmelCase__ : int = self.sigmas[step_index - 1] lowerCAmelCase__ : Any = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API lowerCAmelCase__ : Optional[int] = 0 lowerCAmelCase__ : Union[str, Any] = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": lowerCAmelCase__ : int = sigma_hat if self.state_in_first_order else sigma_next lowerCAmelCase__ : Any = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase__ : Dict = sigma_hat if self.state_in_first_order else sigma_next lowerCAmelCase__ : List[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": lowerCAmelCase__ : int = model_output else: raise ValueError( f'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`''' ) if self.config.clip_sample: lowerCAmelCase__ : str = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order lowerCAmelCase__ : Dict = (sample - pred_original_sample) / sigma_hat # 3. delta timestep lowerCAmelCase__ : Optional[int] = sigma_next - sigma_hat # store for 2nd order step lowerCAmelCase__ : List[Any] = derivative lowerCAmelCase__ : str = dt lowerCAmelCase__ : Dict = sample else: # 2. 2nd order / Heun's method lowerCAmelCase__ : Union[str, Any] = (sample - pred_original_sample) / sigma_next lowerCAmelCase__ : Union[str, Any] = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample lowerCAmelCase__ : Dict = self.dt lowerCAmelCase__ : Optional[int] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" lowerCAmelCase__ : List[str] = None lowerCAmelCase__ : Tuple = None lowerCAmelCase__ : str = None lowerCAmelCase__ : Tuple = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=a ) def _lowerCamelCase ( self : int , a : torch.FloatTensor , a : torch.FloatTensor , a : torch.FloatTensor , ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(a ): # mps does not support float64 lowerCAmelCase__ : Optional[int] = self.timesteps.to(original_samples.device , dtype=torch.floataa ) lowerCAmelCase__ : int = timesteps.to(original_samples.device , dtype=torch.floataa ) else: lowerCAmelCase__ : Union[str, Any] = self.timesteps.to(original_samples.device ) lowerCAmelCase__ : Optional[Any] = timesteps.to(original_samples.device ) lowerCAmelCase__ : List[Any] = [self.index_for_timestep(a , a ) for t in timesteps] lowerCAmelCase__ : List[str] = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): lowerCAmelCase__ : Any = sigma.unsqueeze(-1 ) lowerCAmelCase__ : List[str] = original_samples + noise * sigma return noisy_samples def __len__( self : int ): '''simple docstring''' return self.config.num_train_timesteps
307
0
from datasets.utils.patching import _PatchedModuleObj, patch_submodule from . import _test_patching def lowerCAmelCase__ ( ) -> Dict: import os as original_os from os import path as original_path from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join lowerCAmelCase__ : int = '__test_patch_submodule_mock__' with patch_submodule(_test_patching , 'os.path.join' , _A ): # Every way to access os.path.join must be patched, and the rest must stay untouched # check os.path.join assert isinstance(_test_patching.os , _PatchedModuleObj ) assert isinstance(_test_patching.os.path , _PatchedModuleObj ) assert _test_patching.os.path.join is mock # check path.join assert isinstance(_test_patching.path , _PatchedModuleObj ) assert _test_patching.path.join is mock # check join assert _test_patching.join is mock # check that the other attributes are untouched assert _test_patching.os.rename is original_rename assert _test_patching.path.dirname is original_dirname assert _test_patching.os.path.dirname is original_dirname # Even renamed modules or objects must be patched # check renamed_os.path.join assert isinstance(_test_patching.renamed_os , _PatchedModuleObj ) assert isinstance(_test_patching.renamed_os.path , _PatchedModuleObj ) assert _test_patching.renamed_os.path.join is mock # check renamed_path.join assert isinstance(_test_patching.renamed_path , _PatchedModuleObj ) assert _test_patching.renamed_path.join is mock # check renamed_join assert _test_patching.renamed_join is mock # check that the other attributes are untouched assert _test_patching.renamed_os.rename is original_rename assert _test_patching.renamed_path.dirname is original_dirname assert _test_patching.renamed_os.path.dirname is original_dirname # check that everthing is back to normal when the patch is over assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join def lowerCAmelCase__ ( ) -> Optional[int]: assert _test_patching.open is open lowerCAmelCase__ : str = '__test_patch_submodule_builtin_mock__' # _test_patching has "open" in its globals assert _test_patching.open is open with patch_submodule(_test_patching , 'open' , _A ): assert _test_patching.open is mock # check that everthing is back to normal when the patch is over assert _test_patching.open is open def lowerCAmelCase__ ( ) -> List[str]: lowerCAmelCase__ : str = '__test_patch_submodule_missing_mock__' with patch_submodule(_test_patching , 'pandas.read_csv' , _A ): pass def lowerCAmelCase__ ( ) -> Optional[int]: lowerCAmelCase__ : Union[str, Any] = '__test_patch_submodule_missing_builtin_mock__' # _test_patching doesn't have "len" in its globals assert getattr(_test_patching , 'len' , _A ) is None with patch_submodule(_test_patching , 'len' , _A ): assert _test_patching.len is mock assert _test_patching.len is len def lowerCAmelCase__ ( ) -> Optional[Any]: lowerCAmelCase__ : Optional[Any] = '__test_patch_submodule_start_and_stop_mock__' lowerCAmelCase__ : Optional[Any] = patch_submodule(_test_patching , 'open' , _A ) assert _test_patching.open is open patch.start() assert _test_patching.open is mock patch.stop() assert _test_patching.open is open def lowerCAmelCase__ ( ) -> Dict: from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join lowerCAmelCase__ : Any = '__test_patch_submodule_successive_join__' lowerCAmelCase__ : Optional[int] = '__test_patch_submodule_successive_dirname__' lowerCAmelCase__ : Optional[Any] = '__test_patch_submodule_successive_rename__' assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename with patch_submodule(_test_patching , 'os.path.join' , _A ): with patch_submodule(_test_patching , 'os.rename' , _A ): with patch_submodule(_test_patching , 'os.path.dirname' , _A ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename # try another order with patch_submodule(_test_patching , 'os.rename' , _A ): with patch_submodule(_test_patching , 'os.path.join' , _A ): with patch_submodule(_test_patching , 'os.path.dirname' , _A ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename def lowerCAmelCase__ ( ) -> Union[str, Any]: lowerCAmelCase__ : Any = '__test_patch_submodule_doesnt_exist_mock__' with patch_submodule(_test_patching , '__module_that_doesn_exist__.__attribute_that_doesn_exist__' , _A ): pass with patch_submodule(_test_patching , 'os.__attribute_that_doesn_exist__' , _A ): pass
355
from __future__ import annotations import collections import tempfile import unittest import numpy as np from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import is_tf_available, is_vision_available from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_tf_bert import TFBertModelTester from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester from ..deit.test_modeling_tf_deit import TFDeiTModelTester from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester from ..vit.test_modeling_tf_vit import TFViTModelTester if is_tf_available(): from transformers import ( TFBertModel, TFCLIPVisionModel, TFDeiTModel, TFRobertaModel, TFVisionTextDualEncoderModel, TFViTModel, VisionTextDualEncoderConfig, ) if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: if isinstance(SCREAMING_SNAKE_CASE_ , collections.abc.Iterable ): return x return (x, x) @require_tf class A__ : def _lowerCamelCase ( self : List[Any] , a : List[str] , a : Optional[Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : Dict ): '''simple docstring''' pass def _lowerCamelCase ( self : Dict , a : int , a : str , a : List[Any] , a : Dict , a : List[str]=None , **a : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = VisionTextDualEncoderConfig.from_vision_text_configs(a , a ) lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel(a ) lowerCAmelCase__ : Tuple = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], config.projection_dim) ) def _lowerCamelCase ( self : Union[str, Any] , a : Dict , a : Tuple , a : Dict , a : Union[str, Any] , a : List[Any]=None , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.get_vision_text_model(a , a ) lowerCAmelCase__ : List[Any] = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : Optional[int] = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def _lowerCamelCase ( self : List[str] , a : Optional[int] , a : Optional[int] , a : Union[str, Any] , a : List[Any] , a : Any=None , **a : Dict ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Optional[Any] = {'vision_model': vision_model, 'text_model': text_model} lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel.from_vision_text_pretrained(**a ) lowerCAmelCase__ : Union[str, Any] = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def _lowerCamelCase ( self : Any , a : Optional[int] , a : Optional[int] , a : Dict , a : Optional[int] , a : Optional[int]=None , **a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : int = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Dict = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : List[str] = model(input_ids=a , pixel_values=a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = output[0].numpy() with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(a ) lowerCAmelCase__ : Any = TFVisionTextDualEncoderModel.from_pretrained(a ) lowerCAmelCase__ : int = model(input_ids=a , pixel_values=a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = after_output[0].numpy() lowerCAmelCase__ : Optional[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(a , 1E-5 ) def _lowerCamelCase ( self : List[str] , a : Dict , a : Optional[int] , a : List[Any] , a : str , a : int=None , **a : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Any = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : str = model( input_ids=a , pixel_values=a , attention_mask=a , output_attentions=a ) lowerCAmelCase__ : Union[str, Any] = output.vision_model_output.attentions self.assertEqual(len(a ) , vision_config.num_hidden_layers ) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCAmelCase__ : Optional[int] = to_atuple(vision_model.config.image_size ) lowerCAmelCase__ : Optional[Any] = to_atuple(vision_model.config.patch_size ) lowerCAmelCase__ : List[Any] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase__ : int = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCAmelCase__ : str = output.text_model_output.attentions self.assertEqual(len(a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _lowerCamelCase ( self : List[Any] , a : np.ndarray , a : np.ndarray , a : float ): '''simple docstring''' lowerCAmelCase__ : int = np.abs((a - b) ).max() self.assertLessEqual(a , a , f'''Difference between torch and flax is {diff} (>= {tol}).''' ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_model(**a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Any = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : str = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.prepare_config_and_inputs() self.check_save_load(**a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**a ) @slow def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = self.get_pretrained_model_and_inputs() lowerCAmelCase__ : List[Any] = model_a(**a ) lowerCAmelCase__ : Optional[int] = outputs[0].numpy() with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(a ) lowerCAmelCase__ : str = TFVisionTextDualEncoderModel.from_pretrained(a ) lowerCAmelCase__ : List[str] = model_a(**a ) lowerCAmelCase__ : int = after_outputs[0].numpy() lowerCAmelCase__ : List[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(a , 1E-5 ) @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-vit' , 'hf-internal-testing/tiny-random-bert' ) lowerCAmelCase__ : int = 13 lowerCAmelCase__ : List[Any] = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : int = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Optional[Any] = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : List[Any] = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : List[Any] , a : Dict , a : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = TFViTModel(a , name='vision_model' ) lowerCAmelCase__ : str = TFBertModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = TFViTModelTester(self ) lowerCAmelCase__ : Tuple = TFBertModelTester(self ) lowerCAmelCase__ : Optional[int] = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Union[str, Any] = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : int = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : str = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'Rocketknight1/tiny-random-deit-tf' , 'hf-internal-testing/tiny-random-roberta' ) lowerCAmelCase__ : Tuple = 13 lowerCAmelCase__ : Any = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : Dict = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Any = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : Tuple = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : str , a : Optional[Any] , a : Dict , a : Dict , a : Any , a : Any=None , **a : int ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Optional[int] = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : Any = model( input_ids=a , pixel_values=a , attention_mask=a , output_attentions=a ) lowerCAmelCase__ : Union[str, Any] = output.vision_model_output.attentions self.assertEqual(len(a ) , vision_config.num_hidden_layers ) # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) lowerCAmelCase__ : str = to_atuple(vision_model.config.image_size ) lowerCAmelCase__ : Union[str, Any] = to_atuple(vision_model.config.patch_size ) lowerCAmelCase__ : int = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase__ : int = num_patches + 2 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCAmelCase__ : List[str] = output.text_model_output.attentions self.assertEqual(len(a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _lowerCamelCase ( self : int , a : Optional[int] , a : int ): '''simple docstring''' lowerCAmelCase__ : Dict = TFDeiTModel(a , name='vision_model' ) lowerCAmelCase__ : List[Any] = TFRobertaModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = TFDeiTModelTester(self ) lowerCAmelCase__ : List[str] = TFRobertaModelTester(self ) lowerCAmelCase__ : str = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : List[Any] = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : List[str] = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : Any = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : int = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'Rocketknight1/tiny-random-clip-tf' , 'hf-internal-testing/tiny-random-bert' ) lowerCAmelCase__ : Dict = 13 lowerCAmelCase__ : str = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : List[Any] = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Union[str, Any] = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : Optional[int] = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : str , a : int , a : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = TFCLIPVisionModel(a , name='vision_model' ) lowerCAmelCase__ : List[str] = TFBertModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Any = TFCLIPVisionModelTester(self ) lowerCAmelCase__ : Union[str, Any] = TFBertModelTester(self ) lowerCAmelCase__ : Any = clip_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Any = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ : List[Any] = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : str = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_vision @require_tf class A__ ( unittest.TestCase ): @slow def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel.from_pretrained( 'clip-italian/clip-italian' , logit_scale_init_value=1.0 , from_pt=a ) lowerCAmelCase__ : List[Any] = VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian' ) lowerCAmelCase__ : int = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) lowerCAmelCase__ : Any = processor( text=['una foto di un gatto', 'una foto di un cane'] , images=a , padding=a , return_tensors='np' ) lowerCAmelCase__ : Union[str, Any] = model(**a ) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) ) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) lowerCAmelCase__ : List[str] = np.array([[1.2_2_8_4_7_2_7, 0.3_1_0_4_1_2_2]] ) self.assertTrue(np.allclose(outputs.logits_per_image.numpy() , a , atol=1E-3 ) )
307
0
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device lowerCamelCase__ = False class A__ ( unittest.TestCase ): pass @nightly @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : int ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[Any] = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[Any] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowerCAmelCase__ : List[Any] = torch.manual_seed(0 ) lowerCAmelCase__ : List[str] = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.7_5 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(a ) lowerCAmelCase__ : Optional[int] = VersatileDiffusionPipeline.from_pretrained(a , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Union[str, Any] = generator.manual_seed(0 ) lowerCAmelCase__ : Any = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.7_5 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Any = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = 'cyberpunk 2077' lowerCAmelCase__ : Union[str, Any] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowerCAmelCase__ : Dict = torch.manual_seed(0 ) lowerCAmelCase__ : List[str] = pipe.dual_guided( prompt=a , image=a , text_to_image_strength=0.7_5 , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' , ).images lowerCAmelCase__ : str = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowerCAmelCase__ : List[str] = np.array([0.1_4_4_8, 0.1_6_1_9, 0.1_7_4_1, 0.1_0_8_6, 0.1_1_4_7, 0.1_1_2_8, 0.1_1_9_9, 0.1_1_6_5, 0.1_0_0_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowerCAmelCase__ : str = 'A painting of a squirrel eating a burger ' lowerCAmelCase__ : Any = torch.manual_seed(0 ) lowerCAmelCase__ : Tuple = pipe.text_to_image( prompt=a , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' ).images lowerCAmelCase__ : str = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowerCAmelCase__ : Optional[Any] = np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowerCAmelCase__ : List[str] = pipe.image_variation(a , generator=a , output_type='numpy' ).images lowerCAmelCase__ : Tuple = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowerCAmelCase__ : List[Any] = np.array([0.3_0_7_6, 0.3_1_2_3, 0.3_2_8_4, 0.3_7_8_2, 0.3_7_7_0, 0.3_8_9_4, 0.4_2_9_7, 0.4_3_3_1, 0.4_4_5_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
356
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) lowerCamelCase__ = logging.getLogger(__name__) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : Dict = np.argmax(SCREAMING_SNAKE_CASE_ , axis=1 ) return np.sum(outputs == labels ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: with open(SCREAMING_SNAKE_CASE_ , encoding='utf_8' ) as f: lowerCAmelCase__ : Dict = csv.reader(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = [] next(SCREAMING_SNAKE_CASE_ ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE_ ): output.append((' '.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: lowerCAmelCase__ : Dict = [] for dataset in encoded_datasets: lowerCAmelCase__ : List[str] = len(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase__ : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase__ : List[Any] = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) lowerCAmelCase__ : Tuple = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase__ : Any = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase__ : Optional[Any] = with_conta lowerCAmelCase__ : List[str] = with_conta lowerCAmelCase__ : List[Any] = len(SCREAMING_SNAKE_CASE_ ) - 1 lowerCAmelCase__ : Tuple = len(SCREAMING_SNAKE_CASE_ ) - 1 lowerCAmelCase__ : Tuple = with_conta lowerCAmelCase__ : Optional[int] = with_conta lowerCAmelCase__ : Optional[int] = mc_label lowerCAmelCase__ : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE_ ) for t in all_inputs ) ) return tensor_datasets def lowerCAmelCase__ ( ) -> int: lowerCAmelCase__ : int = argparse.ArgumentParser() parser.add_argument('--model_name' , type=SCREAMING_SNAKE_CASE_ , default='openai-gpt' , help='pretrained model name' ) parser.add_argument('--do_train' , action='store_true' , help='Whether to run training.' ) parser.add_argument('--do_eval' , action='store_true' , help='Whether to run eval on the dev set.' ) parser.add_argument( '--output_dir' , default=SCREAMING_SNAKE_CASE_ , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , help='The output directory where the model predictions and checkpoints will be written.' , ) parser.add_argument('--train_dataset' , type=SCREAMING_SNAKE_CASE_ , default='' ) parser.add_argument('--eval_dataset' , type=SCREAMING_SNAKE_CASE_ , default='' ) parser.add_argument('--seed' , type=SCREAMING_SNAKE_CASE_ , default=42 ) parser.add_argument('--num_train_epochs' , type=SCREAMING_SNAKE_CASE_ , default=3 ) parser.add_argument('--train_batch_size' , type=SCREAMING_SNAKE_CASE_ , default=8 ) parser.add_argument('--eval_batch_size' , type=SCREAMING_SNAKE_CASE_ , default=16 ) parser.add_argument('--adam_epsilon' , default=1e-8 , type=SCREAMING_SNAKE_CASE_ , help='Epsilon for Adam optimizer.' ) parser.add_argument('--max_grad_norm' , type=SCREAMING_SNAKE_CASE_ , default=1 ) parser.add_argument( '--max_steps' , default=-1 , type=SCREAMING_SNAKE_CASE_ , help=( 'If > 0: set total number of training steps to perform. Override num_train_epochs.' ) , ) parser.add_argument( '--gradient_accumulation_steps' , type=SCREAMING_SNAKE_CASE_ , default=1 , help='Number of updates steps to accumulate before performing a backward/update pass.' , ) parser.add_argument('--learning_rate' , type=SCREAMING_SNAKE_CASE_ , default=6.25e-5 ) parser.add_argument('--warmup_steps' , default=0 , type=SCREAMING_SNAKE_CASE_ , help='Linear warmup over warmup_steps.' ) parser.add_argument('--lr_schedule' , type=SCREAMING_SNAKE_CASE_ , default='warmup_linear' ) parser.add_argument('--weight_decay' , type=SCREAMING_SNAKE_CASE_ , default=0.01 ) parser.add_argument('--lm_coef' , type=SCREAMING_SNAKE_CASE_ , default=0.9 ) parser.add_argument('--n_valid' , type=SCREAMING_SNAKE_CASE_ , default=374 ) parser.add_argument('--server_ip' , type=SCREAMING_SNAKE_CASE_ , default='' , help='Can be used for distant debugging.' ) parser.add_argument('--server_port' , type=SCREAMING_SNAKE_CASE_ , default='' , help='Can be used for distant debugging.' ) lowerCAmelCase__ : List[str] = parser.parse_args() print(SCREAMING_SNAKE_CASE_ ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('Waiting for debugger attach' ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=SCREAMING_SNAKE_CASE_ ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) lowerCAmelCase__ : str = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) lowerCAmelCase__ : Dict = torch.cuda.device_count() logger.info('device: {}, n_gpu {}'.format(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) if not args.do_train and not args.do_eval: raise ValueError('At least one of `do_train` or `do_eval` must be True.' ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset lowerCAmelCase__ : Union[str, Any] = ['_start_', '_delimiter_', '_classify_'] lowerCAmelCase__ : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE_ ) ) model.to(SCREAMING_SNAKE_CASE_ ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE_ ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) ) elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE_ ) for o in obj] logger.info('Encoding dataset...' ) lowerCAmelCase__ : List[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase__ : str = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase__ : Union[str, Any] = (train_dataset, eval_dataset) lowerCAmelCase__ : List[str] = tokenize_and_encode(SCREAMING_SNAKE_CASE_ ) # Compute the max input length for the Transformer lowerCAmelCase__ : Union[str, Any] = model.config.n_positions // 2 - 2 lowerCAmelCase__ : Tuple = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) lowerCAmelCase__ : Dict = min(SCREAMING_SNAKE_CASE_ , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase__ : int = pre_process_datasets(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase__ : str = TensorDataset(*SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = RandomSampler(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = DataLoader(SCREAMING_SNAKE_CASE_ , sampler=SCREAMING_SNAKE_CASE_ , batch_size=args.train_batch_size ) lowerCAmelCase__ : Optional[Any] = TensorDataset(*SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = SequentialSampler(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = DataLoader(SCREAMING_SNAKE_CASE_ , sampler=SCREAMING_SNAKE_CASE_ , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase__ : Union[str, Any] = args.max_steps lowerCAmelCase__ : int = args.max_steps // (len(SCREAMING_SNAKE_CASE_ ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase__ : Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase__ : Optional[int] = list(model.named_parameters() ) lowerCAmelCase__ : Tuple = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] lowerCAmelCase__ : str = [ { 'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], 'weight_decay': args.weight_decay, }, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], 'weight_decay': 0.0}, ] lowerCAmelCase__ : Union[str, Any] = AdamW(SCREAMING_SNAKE_CASE_ , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase__ : int = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE_ , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE_ ) if args.do_train: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc='Epoch' ): lowerCAmelCase__ : str = 0 lowerCAmelCase__ : int = 0 lowerCAmelCase__ : str = tqdm(SCREAMING_SNAKE_CASE_ , desc='Training' ) for step, batch in enumerate(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Union[str, Any] = tuple(t.to(SCREAMING_SNAKE_CASE_ ) for t in batch ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Any = batch lowerCAmelCase__ : Tuple = model(SCREAMING_SNAKE_CASE_ , mc_token_ids=SCREAMING_SNAKE_CASE_ , lm_labels=SCREAMING_SNAKE_CASE_ , mc_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : str = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase__ : Optional[int] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase__ : List[str] = 'Training loss: {:.2e} lr: {:.2e}'.format(SCREAMING_SNAKE_CASE_ , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase__ : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE_ , 'module' ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase__ : List[str] = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : List[str] = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE_ ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE_ ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase__ : Dict = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase__ : List[Any] = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE_ ) if args.do_eval: model.eval() lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = 0, 0 lowerCAmelCase__ , lowerCAmelCase__ : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE_ , desc='Evaluating' ): lowerCAmelCase__ : str = tuple(t.to(SCREAMING_SNAKE_CASE_ ) for t in batch ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Tuple = batch with torch.no_grad(): lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : List[str] = model( SCREAMING_SNAKE_CASE_ , mc_token_ids=SCREAMING_SNAKE_CASE_ , lm_labels=SCREAMING_SNAKE_CASE_ , mc_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = mc_logits.detach().cpu().numpy() lowerCAmelCase__ : List[Any] = mc_labels.to('cpu' ).numpy() lowerCAmelCase__ : str = accuracy(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase__ : Optional[int] = eval_loss / nb_eval_steps lowerCAmelCase__ : Any = eval_accuracy / nb_eval_examples lowerCAmelCase__ : Union[str, Any] = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase__ : Tuple = {'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'train_loss': train_loss} lowerCAmelCase__ : Dict = os.path.join(args.output_dir , 'eval_results.txt' ) with open(SCREAMING_SNAKE_CASE_ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key in sorted(result.keys() ): logger.info(' %s = %s' , SCREAMING_SNAKE_CASE_ , str(result[key] ) ) writer.write('%s = %s\n' % (key, str(result[key] )) ) if __name__ == "__main__": main()
307
0
from __future__ import annotations def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Dict: lowerCAmelCase__ : Any = str(SCREAMING_SNAKE_CASE_ ) return len(SCREAMING_SNAKE_CASE_ ) == 9 and set(SCREAMING_SNAKE_CASE_ ) == set('123456789' ) def lowerCAmelCase__ ( ) -> Optional[Any]: for base_num in range(9_999 , 4_999 , -1 ): lowerCAmelCase__ : Any = 100_002 * base_num if is_9_pandigital(SCREAMING_SNAKE_CASE_ ): return candidate for base_num in range(333 , 99 , -1 ): lowerCAmelCase__ : int = 1_002_003 * base_num if is_9_pandigital(SCREAMING_SNAKE_CASE_ ): return candidate return None if __name__ == "__main__": print(F"""{solution() = }""")
357
import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers lowerCamelCase__ = """python tqdm regex requests packaging filelock numpy tokenizers""".split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append("""dataclasses""") if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append("""importlib_metadata""") for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F"""can't find {pkg} in {deps.keys()}, check dependency_versions_table.py""") def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> int: require_version(deps[pkg] , SCREAMING_SNAKE_CASE_ )
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Any: stooge(A__ , 0 , len(A__ ) - 1 ) return arr def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowerCAmelCase__ , lowerCAmelCase__ : Dict = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowerCAmelCase__ : int = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(A__ , A__ , (h - t) ) # Recursively sort last 2/3 elements stooge(A__ , i + t , (A__) ) # Recursively sort first 2/3 elements stooge(A__ , A__ , (h - t) ) if __name__ == "__main__": lowerCamelCase__ = input("""Enter numbers separated by a comma:\n""").strip() lowerCamelCase__ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
358
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class A__ ( __magic_name__ ): lowercase = (DPMSolverSDEScheduler,) lowercase = 10 def _lowerCamelCase ( self : Optional[int] , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = { 'num_train_timesteps': 1_100, 'beta_start': 0.0_0_0_1, 'beta_end': 0.0_2, 'beta_schedule': 'linear', 'noise_sampler_seed': 0, } config.update(**a ) return config def _lowerCamelCase ( self : Tuple ): '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=a ) def _lowerCamelCase ( self : int ): '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=a , beta_end=a ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=a ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = self.scheduler_classes[0] lowerCAmelCase__ : Tuple = self.get_scheduler_config() lowerCAmelCase__ : List[Any] = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Dict = self.dummy_model() lowerCAmelCase__ : int = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : int = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : List[Any] = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : int = scheduler.step(a , a , a ) lowerCAmelCase__ : Any = output.prev_sample lowerCAmelCase__ : List[Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Optional[int] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_7_8_2_1_0_4_4_9_2_1_8_7_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_7_8_7_0_5_9_6_4_5_6_5_2_7_7 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_2_1_1_1_8_1_6_4_0_6 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_6_8_9_2_2_9_9_6_5_2 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.scheduler_classes[0] lowerCAmelCase__ : List[str] = self.get_scheduler_config(prediction_type='v_prediction' ) lowerCAmelCase__ : Any = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Optional[int] = self.dummy_model() lowerCAmelCase__ : Union[str, Any] = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : Any = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : str = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : Dict = scheduler.step(a , a , a ) lowerCAmelCase__ : Tuple = output.prev_sample lowerCAmelCase__ : int = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Union[str, Any] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_2_4.7_7_1_4_9_2_0_0_4_3_9_4_5_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_2_2_6_2_8_9_0_1_4_8_1_6_2_8_4 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_2_8.1_6_6_3_3_6_0_5_9_5_7_0_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_6_8_8_3_2_6_0_0_1_1_6_7_2_9_7 ) < 1E-3 else: assert abs(result_sum.item() - 1_1_9.8_4_8_7_5_4_8_8_2_8_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.1_5_6_0_5_3_0_6_6_2_5_3_6_6_2_1 ) < 1E-3 def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.scheduler_classes[0] lowerCAmelCase__ : Optional[int] = self.get_scheduler_config() lowerCAmelCase__ : int = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : Tuple = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: lowerCAmelCase__ : Dict = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : Optional[int] = model(a , a ) lowerCAmelCase__ : Tuple = scheduler.step(a , a , a ) lowerCAmelCase__ : Dict = output.prev_sample lowerCAmelCase__ : Union[str, Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Dict = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_6_9_5_7_3_9_7_4_6_0_9_3_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_8_0_5_9_3_4_6_0_7_9_8_2_6_3_5 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_3_6_3_7_6_9_5_3_1_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_8_3_8_2_4_1_5_7_7_1 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.scheduler_classes[0] lowerCAmelCase__ : Any = self.get_scheduler_config() lowerCAmelCase__ : Any = scheduler_class(**a , use_karras_sigmas=a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : str = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma lowerCAmelCase__ : str = sample.to(a ) for t in scheduler.timesteps: lowerCAmelCase__ : Any = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : int = model(a , a ) lowerCAmelCase__ : Union[str, Any] = scheduler.step(a , a , a ) lowerCAmelCase__ : Union[str, Any] = output.prev_sample lowerCAmelCase__ : Optional[int] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Any = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_7_6.6_6_9_7_4_1_3_5_7_4_2_1_8_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_7.6_3_6_5_3_5_6_4_4_5_3_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 else: assert abs(result_sum.item() - 1_7_0.3_1_3_5_2_2_3_3_8_8_6_7_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2
307
0
import math def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> int: if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): lowerCAmelCase__ : Optional[Any] = F'''Input value of [number={number}] must be an integer''' raise TypeError(_UpperCAmelCase ) if number < 1: lowerCAmelCase__ : int = F'''Input value of [number={number}] must be > 0''' raise ValueError(_UpperCAmelCase ) elif number == 1: return 3 elif number == 2: return 5 else: lowerCAmelCase__ : Optional[int] = int(math.log(number // 3 , 2 ) ) + 2 lowerCAmelCase__ : str = [3, 5] lowerCAmelCase__ : str = 2 lowerCAmelCase__ : Optional[Any] = 3 for block in range(1 , _UpperCAmelCase ): for _ in range(_UpperCAmelCase ): proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1] ) proth_index += 1 increment *= 2 return proth_list[number - 1] if __name__ == "__main__": import doctest doctest.testmod() for number in range(11): lowerCamelCase__ = 0 try: lowerCamelCase__ = proth(number) except ValueError: print(F"""ValueError: there is no {number}th Proth number""") continue print(F"""The {number}th Proth number: {value}""")
359
import os import string import sys lowerCamelCase__ = 1 << 8 lowerCamelCase__ = { """tab""": ord("""\t"""), """newline""": ord("""\r"""), """esc""": 27, """up""": 65 + ARROW_KEY_FLAG, """down""": 66 + ARROW_KEY_FLAG, """right""": 67 + ARROW_KEY_FLAG, """left""": 68 + ARROW_KEY_FLAG, """mod_int""": 91, """undefined""": sys.maxsize, """interrupt""": 3, """insert""": 50, """delete""": 51, """pg_up""": 53, """pg_down""": 54, } lowerCamelCase__ = KEYMAP["""up"""] lowerCamelCase__ = KEYMAP["""left"""] if sys.platform == "win32": lowerCamelCase__ = [] lowerCamelCase__ = { b"""\xe0H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\x00H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\xe0P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\x00P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\xe0M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\x00M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\xe0K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, b"""\x00K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, } for i in range(10): lowerCamelCase__ = ord(str(i)) def lowerCAmelCase__ ( ) -> Dict: if os.name == "nt": import msvcrt lowerCAmelCase__ : Dict = 'mbcs' # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(SCREAMING_SNAKE_CASE_ ) == 0: # Read the keystroke lowerCAmelCase__ : Optional[Any] = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): lowerCAmelCase__ : Dict = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: lowerCAmelCase__ : Dict = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP['mod_int'] ) ) WIN_CH_BUFFER.append(SCREAMING_SNAKE_CASE_ ) if ord(SCREAMING_SNAKE_CASE_ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) lowerCAmelCase__ : Dict = chr(KEYMAP['esc'] ) except KeyError: lowerCAmelCase__ : Dict = cha[1] else: lowerCAmelCase__ : List[Any] = ch.decode(SCREAMING_SNAKE_CASE_ ) else: lowerCAmelCase__ : Tuple = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty lowerCAmelCase__ : Tuple = sys.stdin.fileno() lowerCAmelCase__ : Any = termios.tcgetattr(SCREAMING_SNAKE_CASE_ ) try: tty.setraw(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = sys.stdin.read(1 ) finally: termios.tcsetattr(SCREAMING_SNAKE_CASE_ , termios.TCSADRAIN , SCREAMING_SNAKE_CASE_ ) return ch def lowerCAmelCase__ ( ) -> Union[str, Any]: lowerCAmelCase__ : Any = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["esc"]: lowerCAmelCase__ : Union[str, Any] = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["mod_int"]: lowerCAmelCase__ : str = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(SCREAMING_SNAKE_CASE_ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(SCREAMING_SNAKE_CASE_ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
307
0
"""simple docstring""" import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class A__ ( _lowercase ): lowercase = ['image_processor', 'tokenizer'] lowercase = 'OwlViTImageProcessor' lowercase = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : Any , a : int=None , a : Optional[Any]=None , **a : List[str] ): '''simple docstring''' lowerCAmelCase__ : Tuple = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , __UpperCamelCase , ) lowerCAmelCase__ : List[str] = kwargs.pop('feature_extractor' ) lowerCAmelCase__ : List[str] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(__UpperCamelCase , __UpperCamelCase ) def __call__( self : List[str] , a : Dict=None , a : List[str]=None , a : Tuple=None , a : Optional[Any]="max_length" , a : List[Any]="np" , **a : Optional[int] ): '''simple docstring''' if text is None and query_images is None and images is None: raise ValueError( 'You have to specify at least one text or query image or image. All three cannot be none.' ) if text is not None: if isinstance(__UpperCamelCase , __UpperCamelCase ) or (isinstance(__UpperCamelCase , __UpperCamelCase ) and not isinstance(text[0] , __UpperCamelCase )): lowerCAmelCase__ : List[str] = [self.tokenizer(__UpperCamelCase , padding=__UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase )] elif isinstance(__UpperCamelCase , __UpperCamelCase ) and isinstance(text[0] , __UpperCamelCase ): lowerCAmelCase__ : List[Any] = [] # Maximum number of queries across batch lowerCAmelCase__ : int = max([len(__UpperCamelCase ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(__UpperCamelCase ) != max_num_queries: lowerCAmelCase__ : List[str] = t + [' '] * (max_num_queries - len(__UpperCamelCase )) lowerCAmelCase__ : Any = self.tokenizer(__UpperCamelCase , padding=__UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase ) encodings.append(__UpperCamelCase ) else: raise TypeError('Input text should be a string, a list of strings or a nested list of strings' ) if return_tensors == "np": lowerCAmelCase__ : int = np.concatenate([encoding['input_ids'] for encoding in encodings] , axis=0 ) lowerCAmelCase__ : Dict = np.concatenate([encoding['attention_mask'] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp lowerCAmelCase__ : List[Any] = jnp.concatenate([encoding['input_ids'] for encoding in encodings] , axis=0 ) lowerCAmelCase__ : Optional[int] = jnp.concatenate([encoding['attention_mask'] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch lowerCAmelCase__ : int = torch.cat([encoding['input_ids'] for encoding in encodings] , dim=0 ) lowerCAmelCase__ : Optional[int] = torch.cat([encoding['attention_mask'] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf lowerCAmelCase__ : Optional[int] = tf.stack([encoding['input_ids'] for encoding in encodings] , axis=0 ) lowerCAmelCase__ : Tuple = tf.stack([encoding['attention_mask'] for encoding in encodings] , axis=0 ) else: raise ValueError('Target return tensor type could not be returned' ) lowerCAmelCase__ : Tuple = BatchEncoding() lowerCAmelCase__ : str = input_ids lowerCAmelCase__ : List[str] = attention_mask if query_images is not None: lowerCAmelCase__ : Optional[Any] = BatchEncoding() lowerCAmelCase__ : Optional[Any] = self.image_processor( __UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase ).pixel_values lowerCAmelCase__ : List[Any] = query_pixel_values if images is not None: lowerCAmelCase__ : Tuple = self.image_processor(__UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase ) if text is not None and images is not None: lowerCAmelCase__ : List[str] = image_features.pixel_values return encoding elif query_images is not None and images is not None: lowerCAmelCase__ : List[str] = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**__UpperCamelCase ) , tensor_type=__UpperCamelCase ) def _lowerCamelCase ( self : str , *a : str , **a : str ): '''simple docstring''' return self.image_processor.post_process(*__UpperCamelCase , **__UpperCamelCase ) def _lowerCamelCase ( self : str , *a : Optional[Any] , **a : Optional[Any] ): '''simple docstring''' return self.image_processor.post_process_object_detection(*__UpperCamelCase , **__UpperCamelCase ) def _lowerCamelCase ( self : List[Any] , *a : List[str] , **a : Optional[Any] ): '''simple docstring''' return self.image_processor.post_process_image_guided_detection(*__UpperCamelCase , **__UpperCamelCase ) def _lowerCamelCase ( self : Tuple , *a : Optional[Any] , **a : Any ): '''simple docstring''' return self.tokenizer.batch_decode(*__UpperCamelCase , **__UpperCamelCase ) def _lowerCamelCase ( self : Optional[int] , *a : Tuple , **a : List[Any] ): '''simple docstring''' return self.tokenizer.decode(*__UpperCamelCase , **__UpperCamelCase ) @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' , __UpperCamelCase , ) return self.image_processor_class @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' , __UpperCamelCase , ) return self.image_processor
360
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: return "".join([hex(SCREAMING_SNAKE_CASE_ )[2:].zfill(2 ).upper() for byte in list(SCREAMING_SNAKE_CASE_ )] ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> bytes: # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(SCREAMING_SNAKE_CASE_ ) % 2) != 0: raise ValueError( 'Base16 encoded data is invalid:\nData does not have an even number of hex digits.' ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(SCREAMING_SNAKE_CASE_ ) <= set('0123456789ABCDEF' ): raise ValueError( 'Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.' ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
307
0
import operator def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = False , SCREAMING_SNAKE_CASE_ = None ) -> List[Any]: lowerCAmelCase__ : Dict = operator.lt if reverse else operator.gt lowerCAmelCase__ : int = solution or [] if not arr: return solution lowerCAmelCase__ : int = [arr.pop(0 )] for i, item in enumerate(SCREAMING_SNAKE_CASE_ ): if _operator(SCREAMING_SNAKE_CASE_ , sublist[-1] ): sublist.append(SCREAMING_SNAKE_CASE_ ) arr.pop(SCREAMING_SNAKE_CASE_ ) # merging sublist into solution list if not solution: solution.extend(SCREAMING_SNAKE_CASE_ ) else: while sublist: lowerCAmelCase__ : Optional[int] = sublist.pop(0 ) for i, xx in enumerate(SCREAMING_SNAKE_CASE_ ): if not _operator(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): solution.insert(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) break else: solution.append(SCREAMING_SNAKE_CASE_ ) strand_sort(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
361
from __future__ import annotations def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[list[int]]: lowerCAmelCase__ : list[list[int]] = [] create_all_state(1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , [] , SCREAMING_SNAKE_CASE_ ) return result def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) -> None: if level == 0: total_list.append(current_list[:] ) return for i in range(SCREAMING_SNAKE_CASE_ , total_number - level + 2 ): current_list.append(SCREAMING_SNAKE_CASE_ ) create_all_state(i + 1 , SCREAMING_SNAKE_CASE_ , level - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) current_list.pop() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> None: for i in total_list: print(*SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCamelCase__ = 4 lowerCamelCase__ = 2 lowerCamelCase__ = generate_all_combinations(n, k) print_all_state(total_list)
307
0
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCamelCase__ = logging.get_logger(__name__) # pylint: disable=invalid-name class A__ ( UpperCamelCase_ , UpperCamelCase_ ): @register_to_config def __init__( self : List[Any] , a : str , a : Optional[Any] = None , a : Dict = None ): '''simple docstring''' super().__init__() lowerCAmelCase__ : List[Any] = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" lowerCAmelCase__ : Union[str, Any] = torch.zeros(_a , _a ) else: lowerCAmelCase__ : Any = None lowerCAmelCase__ : int = torch.nn.Parameter(_a ) class A__ ( UpperCamelCase_ ): lowercase = 42 lowercase = 42 lowercase = 42 lowercase = 42 lowercase = 42 lowercase = 42 def __init__( self : Dict , a : Optional[Any] , a : Tuple , a : int , a : int , a : Any , a : Tuple , ): '''simple docstring''' super().__init__() self.register_modules( vqvae=_a , transformer=_a , text_encoder=_a , tokenizer=_a , scheduler=_a , learned_classifier_free_sampling_embeddings=_a , ) def _lowerCamelCase ( self : Optional[Any] , a : Union[str, Any] , a : Any , a : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = len(_a ) if isinstance(_a , _a ) else 1 # get prompt text embeddings lowerCAmelCase__ : Optional[Any] = self.tokenizer( _a , padding='max_length' , max_length=self.tokenizer.model_max_length , return_tensors='pt' , ) lowerCAmelCase__ : Any = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: lowerCAmelCase__ : List[str] = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( 'The following part of your input was truncated because CLIP can only handle sequences up to' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) lowerCAmelCase__ : int = text_input_ids[:, : self.tokenizer.model_max_length] lowerCAmelCase__ : Dict = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 lowerCAmelCase__ : List[str] = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=_a ) # duplicate text embeddings for each generation per prompt lowerCAmelCase__ : Dict = prompt_embeds.repeat_interleave(_a , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: lowerCAmelCase__ : int = self.learned_classifier_free_sampling_embeddings.embeddings lowerCAmelCase__ : int = negative_prompt_embeds.unsqueeze(0 ).repeat(_a , 1 , 1 ) else: lowerCAmelCase__ : Tuple = [""""""] * batch_size lowerCAmelCase__ : List[Any] = text_input_ids.shape[-1] lowerCAmelCase__ : str = self.tokenizer( _a , padding='max_length' , max_length=_a , truncation=_a , return_tensors='pt' , ) lowerCAmelCase__ : int = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings lowerCAmelCase__ : int = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=_a ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method lowerCAmelCase__ : List[str] = negative_prompt_embeds.shape[1] lowerCAmelCase__ : Optional[int] = negative_prompt_embeds.repeat(1 , _a , 1 ) lowerCAmelCase__ : Optional[int] = negative_prompt_embeds.view(batch_size * num_images_per_prompt , _a , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes lowerCAmelCase__ : List[Any] = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self : Union[str, Any] , a : List[Any] , a : Any = 100 , a : Dict = 5.0 , a : Dict = 1.0 , a : Tuple = 1 , a : int = None , a : Union[str, Any] = None , a : List[str] = "pil" , a : Tuple = True , a : Union[str, Any] = None , a : Optional[Any] = 1 , ): '''simple docstring''' if isinstance(_a , _a ): lowerCAmelCase__ : List[str] = 1 elif isinstance(_a , _a ): lowerCAmelCase__ : Tuple = len(_a ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(_a )}''' ) lowerCAmelCase__ : str = batch_size * num_images_per_prompt lowerCAmelCase__ : Dict = guidance_scale > 1.0 lowerCAmelCase__ : Union[str, Any] = self._encode_prompt(_a , _a , _a ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_a , _a ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(_a )}.''' ) # get the initial completely masked latents unless the user supplied it lowerCAmelCase__ : str = (batch_size, self.transformer.num_latent_pixels) if latents is None: lowerCAmelCase__ : List[str] = self.transformer.num_vector_embeds - 1 lowerCAmelCase__ : Optional[int] = torch.full(_a , _a ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( 'Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) lowerCAmelCase__ : Optional[int] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(_a , device=self.device ) lowerCAmelCase__ : int = self.scheduler.timesteps.to(self.device ) lowerCAmelCase__ : Union[str, Any] = latents for i, t in enumerate(self.progress_bar(_a ) ): # expand the sample if we are doing classifier free guidance lowerCAmelCase__ : int = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` lowerCAmelCase__ : Dict = self.transformer(_a , encoder_hidden_states=_a , timestep=_a ).sample if do_classifier_free_guidance: lowerCAmelCase__ : Optional[int] = model_output.chunk(2 ) lowerCAmelCase__ : Dict = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(_a , dim=1 , keepdim=_a ) lowerCAmelCase__ : str = self.truncate(_a , _a ) # remove `log(0)`'s (`-inf`s) lowerCAmelCase__ : Optional[Any] = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 lowerCAmelCase__ : Optional[int] = self.scheduler.step(_a , timestep=_a , sample=_a , generator=_a ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_a , _a , _a ) lowerCAmelCase__ : Dict = self.vqvae.config.vq_embed_dim lowerCAmelCase__ : str = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) lowerCAmelCase__ : str = self.vqvae.quantize.get_codebook_entry(_a , shape=_a ) lowerCAmelCase__ : Dict = self.vqvae.decode(_a , force_not_quantize=_a ).sample lowerCAmelCase__ : str = (image / 2 + 0.5).clamp(0 , 1 ) lowerCAmelCase__ : str = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": lowerCAmelCase__ : Union[str, Any] = self.numpy_to_pil(_a ) if not return_dict: return (image,) return ImagePipelineOutput(images=_a ) def _lowerCamelCase ( self : Tuple , a : Any , a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : List[Any] = torch.sort(_a , 1 , descending=_a ) lowerCAmelCase__ : Tuple = torch.exp(_a ) lowerCAmelCase__ : str = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out lowerCAmelCase__ : List[Any] = torch.full_like(keep_mask[:, 0:1, :] , _a ) lowerCAmelCase__ : Optional[Any] = torch.cat((all_true, keep_mask) , dim=1 ) lowerCAmelCase__ : Optional[Any] = keep_mask[:, :-1, :] lowerCAmelCase__ : List[Any] = keep_mask.gather(1 , indices.argsort(1 ) ) lowerCAmelCase__ : int = log_p_x_0.clone() lowerCAmelCase__ : Union[str, Any] = -torch.inf # -inf = log(0) return rv
362
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class A__ ( unittest.TestCase ): @parameterized.expand([(None,), ('foo.json',)] ) def _lowerCamelCase ( self : Dict , a : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a , config_name=a ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(a , config_name=a ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 50 ) self.assertEqual(loaded_config.max_length , 20 ) self.assertEqual(loaded_config.max_time , a ) def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Dict = AutoConfig.from_pretrained('gpt2' ) lowerCAmelCase__ : Any = GenerationConfig.from_model_config(a ) lowerCAmelCase__ : Any = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(a , a ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : Dict = { 'max_new_tokens': 1_024, 'foo': 'bar', } lowerCAmelCase__ : List[Any] = copy.deepcopy(a ) lowerCAmelCase__ : Dict = generation_config.update(**a ) # update_kwargs was not modified (no side effects) self.assertEqual(a , a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_024 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(a , {'foo': 'bar'} ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : List[Any] = 'bar' with tempfile.TemporaryDirectory('test-generation-config' ) as tmp_dir: generation_config.save_pretrained(a ) lowerCAmelCase__ : List[Any] = GenerationConfig.from_pretrained(a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , 'bar' ) lowerCAmelCase__ : int = GenerationConfig.from_model_config(a ) assert not hasattr(a , 'foo' ) # no new kwargs should be initialized if from config def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , a ) self.assertEqual(default_config.num_beams , 1 ) lowerCAmelCase__ : List[Any] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , a ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(a , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class A__ ( unittest.TestCase ): @classmethod def _lowerCamelCase ( cls : int ): '''simple docstring''' lowerCAmelCase__ : List[str] = TOKEN HfFolder.save_token(a ) @classmethod def _lowerCamelCase ( cls : Optional[int] ): '''simple docstring''' try: delete_repo(token=cls._token , repo_id='test-generation-config' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-generation-config-org' ) except HTTPError: pass def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('test-generation-config' , use_auth_token=self._token ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='test-generation-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='test-generation-config' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('valid_org/test-generation-config-org' , use_auth_token=self._token ) lowerCAmelCase__ : Dict = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-generation-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='valid_org/test-generation-config-org' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : List[str] = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) )
307
0
import warnings from typing import Dict, List, Optional, Tuple from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) class A__ ( _A ): lowercase = ['input_ids', 'attention_mask'] def __init__( self : Dict , a : Tuple="</s>" , a : Optional[int]="<unk>" , a : List[Any]="<pad>" , a : List[Any]=125 , a : Dict=None , **a : str , ): '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: lowerCAmelCase__ : Optional[Any] = [f'''<extra_id_{i}>''' for i in range(__SCREAMING_SNAKE_CASE )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens lowerCAmelCase__ : Union[str, Any] = len(set(filter(lambda a : bool('extra_id' in str(__SCREAMING_SNAKE_CASE ) ) , __SCREAMING_SNAKE_CASE ) ) ) if extra_tokens != extra_ids: raise ValueError( f'''Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are''' ' provided to ByT5Tokenizer. In this case the additional_special_tokens must include the' ' extra_ids tokens' ) lowerCAmelCase__ : int = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else pad_token lowerCAmelCase__ : Any = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else eos_token lowerCAmelCase__ : Tuple = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else unk_token super().__init__( eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , extra_ids=__SCREAMING_SNAKE_CASE , additional_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) lowerCAmelCase__ : int = extra_ids lowerCAmelCase__ : int = 2**8 # utf is 8 bits # define special tokens dict lowerCAmelCase__ : Tuple = { self.pad_token: 0, self.eos_token: 1, self.unk_token: 2, } lowerCAmelCase__ : Union[str, Any] = len(self.special_tokens_encoder ) lowerCAmelCase__ : List[Any] = len(__SCREAMING_SNAKE_CASE ) for i, token in enumerate(__SCREAMING_SNAKE_CASE ): lowerCAmelCase__ : Tuple = self.vocab_size + i - n lowerCAmelCase__ : Optional[Any] = {v: k for k, v in self.special_tokens_encoder.items()} @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return self._utf_vocab_size + self._num_special_tokens + self._extra_ids def _lowerCamelCase ( self : Dict , a : List[int] , a : Optional[List[int]] = None , a : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__SCREAMING_SNAKE_CASE , token_ids_a=__SCREAMING_SNAKE_CASE , already_has_special_tokens=__SCREAMING_SNAKE_CASE ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def _lowerCamelCase ( self : Optional[Any] , a : List[int] ): '''simple docstring''' if len(__SCREAMING_SNAKE_CASE ) > 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 : Optional[Any] , a : List[int] , a : Optional[List[int]] = None ): '''simple docstring''' lowerCAmelCase__ : List[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 : int , a : List[int] , a : Optional[List[int]] = None ): '''simple docstring''' lowerCAmelCase__ : List[Any] = self._add_eos_if_not_present(__SCREAMING_SNAKE_CASE ) if token_ids_a is None: return token_ids_a else: lowerCAmelCase__ : str = self._add_eos_if_not_present(__SCREAMING_SNAKE_CASE ) return token_ids_a + token_ids_a def _lowerCamelCase ( self : Tuple , a : str ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = [chr(__SCREAMING_SNAKE_CASE ) for i in text.encode('utf-8' )] return tokens def _lowerCamelCase ( self : str , a : Tuple ): '''simple docstring''' if token in self.special_tokens_encoder: lowerCAmelCase__ : Optional[int] = self.special_tokens_encoder[token] elif token in self.added_tokens_encoder: lowerCAmelCase__ : Optional[Any] = self.added_tokens_encoder[token] elif len(__SCREAMING_SNAKE_CASE ) != 1: lowerCAmelCase__ : Optional[Any] = self.unk_token_id else: lowerCAmelCase__ : Optional[Any] = ord(__SCREAMING_SNAKE_CASE ) + self._num_special_tokens return token_id def _lowerCamelCase ( self : Optional[int] , a : Union[str, Any] ): '''simple docstring''' if index in self.special_tokens_decoder: lowerCAmelCase__ : Optional[int] = self.special_tokens_decoder[index] else: lowerCAmelCase__ : Union[str, Any] = chr(index - self._num_special_tokens ) return token def _lowerCamelCase ( self : Optional[Any] , a : Dict ): '''simple docstring''' lowerCAmelCase__ : List[Any] = b'' for token in tokens: if token in self.special_tokens_decoder: lowerCAmelCase__ : Optional[Any] = self.special_tokens_decoder[token].encode('utf-8' ) elif token in self.added_tokens_decoder: lowerCAmelCase__ : str = self.special_tokens_decoder[token].encode('utf-8' ) elif token in self.special_tokens_encoder: lowerCAmelCase__ : Dict = token.encode('utf-8' ) elif token in self.added_tokens_encoder: lowerCAmelCase__ : List[str] = token.encode('utf-8' ) else: lowerCAmelCase__ : List[str] = bytes([ord(__SCREAMING_SNAKE_CASE )] ) bstring += tok_string lowerCAmelCase__ : int = bstring.decode('utf-8' , errors='ignore' ) return string def _lowerCamelCase ( self : Dict , a : str , a : Optional[str] = None ): '''simple docstring''' return ()
363
import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import ( DiffusionPipeline, UnCLIPImageVariationPipeline, UnCLIPScheduler, UNetaDConditionModel, UNetaDModel, ) from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, load_image, require_torch_gpu, skip_mps from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = UnCLIPImageVariationPipeline lowercase = IMAGE_VARIATION_PARAMS - {'height', 'width', 'guidance_scale'} lowercase = IMAGE_VARIATION_BATCH_PARAMS lowercase = [ 'generator', 'return_dict', 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] lowercase = False @property def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : int ): '''simple docstring''' return self.time_input_dim @property def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return self.time_input_dim * 4 @property def _lowerCamelCase ( self : Any ): '''simple docstring''' return 100 @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) return tokenizer @property def _lowerCamelCase ( self : Any ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) return CLIPTextModelWithProjection(a ) @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : List[Any] = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) return CLIPVisionModelWithProjection(a ) @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Union[str, Any] = { 'clip_embeddings_dim': self.text_embedder_hidden_size, 'time_embed_dim': self.time_embed_dim, 'cross_attention_dim': self.cross_attention_dim, } lowerCAmelCase__ : Optional[Any] = UnCLIPTextProjModel(**a ) return model @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : str = { 'sample_size': 32, # RGB in channels 'in_channels': 3, # Out channels is double in channels because predicts mean and variance 'out_channels': 6, 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': 'identity', } lowerCAmelCase__ : str = UNetaDConditionModel(**a ) return model @property def _lowerCamelCase ( self : str ): '''simple docstring''' return { "sample_size": 64, "layers_per_block": 1, "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"), "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"), "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "in_channels": 6, "out_channels": 3, } @property def _lowerCamelCase ( self : str ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Any = UNetaDModel(**self.dummy_super_res_kwargs ) return model @property def _lowerCamelCase ( self : int ): '''simple docstring''' torch.manual_seed(1 ) lowerCAmelCase__ : List[str] = UNetaDModel(**self.dummy_super_res_kwargs ) return model def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.dummy_decoder lowerCAmelCase__ : Optional[int] = self.dummy_text_proj lowerCAmelCase__ : Any = self.dummy_text_encoder lowerCAmelCase__ : Any = self.dummy_tokenizer lowerCAmelCase__ : Any = self.dummy_super_res_first lowerCAmelCase__ : Optional[int] = self.dummy_super_res_last lowerCAmelCase__ : Dict = UnCLIPScheduler( variance_type='learned_range' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = UnCLIPScheduler( variance_type='fixed_small_log' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = CLIPImageProcessor(crop_size=32 , size=32 ) lowerCAmelCase__ : Optional[int] = self.dummy_image_encoder return { "decoder": decoder, "text_encoder": text_encoder, "tokenizer": tokenizer, "text_proj": text_proj, "feature_extractor": feature_extractor, "image_encoder": image_encoder, "super_res_first": super_res_first, "super_res_last": super_res_last, "decoder_scheduler": decoder_scheduler, "super_res_scheduler": super_res_scheduler, } def _lowerCamelCase ( self : Any , a : Dict , a : List[str]=0 , a : List[str]=True ): '''simple docstring''' lowerCAmelCase__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(a ) ).to(a ) if str(a ).startswith('mps' ): lowerCAmelCase__ : Optional[int] = torch.manual_seed(a ) else: lowerCAmelCase__ : str = torch.Generator(device=a ).manual_seed(a ) if pil_image: lowerCAmelCase__ : Optional[int] = input_image * 0.5 + 0.5 lowerCAmelCase__ : Dict = input_image.clamp(0 , 1 ) lowerCAmelCase__ : List[Any] = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() lowerCAmelCase__ : Union[str, Any] = DiffusionPipeline.numpy_to_pil(a )[0] return { "image": input_image, "generator": generator, "decoder_num_inference_steps": 2, "super_res_num_inference_steps": 2, "output_type": "np", } def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = 'cpu' lowerCAmelCase__ : Any = self.get_dummy_components() lowerCAmelCase__ : List[str] = self.pipeline_class(**a ) lowerCAmelCase__ : Dict = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : str = pipe(**a ) lowerCAmelCase__ : Optional[Any] = output.images lowerCAmelCase__ : str = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : List[str] = np.array( [ 0.9_9_9_7, 0.0_0_0_2, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_6_9, 0.0_0_2_3, 0.9_9_9_7, 0.9_9_6_9, 0.9_9_7_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 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = 'cpu' lowerCAmelCase__ : Dict = self.get_dummy_components() lowerCAmelCase__ : Optional[int] = self.pipeline_class(**a ) lowerCAmelCase__ : int = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = pipe(**a ) lowerCAmelCase__ : Union[str, Any] = output.images lowerCAmelCase__ : int = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : int = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Tuple = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : str = np.array([0.9_9_9_7, 0.0_0_0_3, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_7_0, 0.0_0_2_4, 0.9_9_9_7, 0.9_9_7_1, 0.9_9_7_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Tuple = 'cpu' lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : Tuple = self.pipeline_class(**a ) lowerCAmelCase__ : Union[str, Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Tuple = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = [ pipeline_inputs['image'], pipeline_inputs['image'], ] lowerCAmelCase__ : Optional[int] = pipe(**a ) lowerCAmelCase__ : Tuple = output.images lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Union[str, Any] = [ tuple_pipeline_inputs['image'], tuple_pipeline_inputs['image'], ] lowerCAmelCase__ : str = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Any = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (2, 64, 64, 3) lowerCAmelCase__ : Union[str, Any] = np.array( [ 0.9_9_9_7, 0.9_9_8_9, 0.0_0_0_8, 0.0_0_2_1, 0.9_9_6_0, 0.0_0_1_8, 0.0_0_1_4, 0.0_0_0_2, 0.9_9_3_3, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch.device('cpu' ) class A__ : lowercase = 1 lowerCAmelCase__ : Optional[Any] = self.get_dummy_components() lowerCAmelCase__ : Dict = self.pipeline_class(**a ) lowerCAmelCase__ : Optional[Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.Generator(device=a ).manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe.decoder.dtype lowerCAmelCase__ : Union[str, Any] = 1 lowerCAmelCase__ : str = ( batch_size, pipe.decoder.config.in_channels, pipe.decoder.config.sample_size, pipe.decoder.config.sample_size, ) lowerCAmelCase__ : List[Any] = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[str] = ( batch_size, pipe.super_res_first.config.in_channels // 2, pipe.super_res_first.config.sample_size, pipe.super_res_first.config.sample_size, ) lowerCAmelCase__ : Any = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , decoder_latents=a , super_res_latents=a ).images lowerCAmelCase__ : Optional[Any] = self.get_dummy_inputs(a , pil_image=a ) # Don't pass image, instead pass embedding lowerCAmelCase__ : Union[str, Any] = pipeline_inputs.pop('image' ) lowerCAmelCase__ : Union[str, Any] = pipe.image_encoder(a ).image_embeds lowerCAmelCase__ : List[Any] = pipe( **a , decoder_latents=a , super_res_latents=a , image_embeddings=a , ).images # make sure passing text embeddings manually is identical assert np.abs(img_out_a - img_out_a ).max() < 1E-4 @skip_mps def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device == 'cpu' # Check is relaxed because there is not a torch 2.0 sliced attention added kv processor lowerCAmelCase__ : int = 1E-2 self._test_attention_slicing_forward_pass( test_max_difference=a , expected_max_diff=a ) @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = torch_device == 'cpu' lowerCAmelCase__ : Any = True lowerCAmelCase__ : Optional[Any] = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] self._test_inference_batch_single_identical( test_max_difference=a , relax_max_difference=a , additional_params_copy_to_batched_inputs=a , ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] if torch_device == "mps": # TODO: MPS errors with larger batch sizes lowerCAmelCase__ : List[str] = [2, 3] self._test_inference_batch_consistent( batch_sizes=a , additional_params_copy_to_batched_inputs=a , ) else: self._test_inference_batch_consistent( additional_params_copy_to_batched_inputs=a ) @skip_mps def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent() @skip_mps def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_optional_components() @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Tuple ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/unclip/cat.png' ) lowerCAmelCase__ : List[Any] = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/unclip/karlo_v1_alpha_cat_variation_fp16.npy' ) lowerCAmelCase__ : Tuple = UnCLIPImageVariationPipeline.from_pretrained( 'kakaobrain/karlo-v1-alpha-image-variations' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Union[str, Any] = pipeline.to(a ) pipeline.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = torch.Generator(device='cpu' ).manual_seed(0 ) lowerCAmelCase__ : List[str] = pipeline( a , generator=a , output_type='np' , ) lowerCAmelCase__ : Union[str, Any] = output.images[0] assert image.shape == (256, 256, 3) assert_mean_pixel_difference(a , a , 15 )
307
0
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( HubertConfig, HubertForCTC, HubertModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.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.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', } def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: for attribute in key.split('.' ): lowerCAmelCase__ : Optional[Any] = getattr(__snake_case , __snake_case ) if weight_type is not None: lowerCAmelCase__ : Optional[int] = getattr(__snake_case , __snake_case ).shape else: lowerCAmelCase__ : Tuple = 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": lowerCAmelCase__ : Dict = value elif weight_type == "weight_g": lowerCAmelCase__ : Tuple = value elif weight_type == "weight_v": lowerCAmelCase__ : Union[str, Any] = value elif weight_type == "bias": lowerCAmelCase__ : Optional[Any] = value else: lowerCAmelCase__ : Tuple = value logger.info(F'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: lowerCAmelCase__ : Optional[int] = [] lowerCAmelCase__ : Tuple = fairseq_model.state_dict() lowerCAmelCase__ : List[str] = hf_model.hubert.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): lowerCAmelCase__ : Union[str, Any] = False if "conv_layers" in name: load_conv_layer( __snake_case , __snake_case , __snake_case , __snake_case , hf_model.config.feat_extract_norm == 'group' , ) lowerCAmelCase__ : List[str] = True else: for key, mapped_key in MAPPING.items(): lowerCAmelCase__ : List[Any] = "hubert." + 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] and not is_finetuned): lowerCAmelCase__ : List[str] = True if "*" in mapped_key: lowerCAmelCase__ : Optional[int] = name.split(__snake_case )[0].split('.' )[-2] lowerCAmelCase__ : Tuple = mapped_key.replace('*' , __snake_case ) if "weight_g" in name: lowerCAmelCase__ : Any = "weight_g" elif "weight_v" in name: lowerCAmelCase__ : Optional[Any] = "weight_v" elif "weight" in name: lowerCAmelCase__ : Optional[int] = "weight" elif "bias" in name: lowerCAmelCase__ : Optional[Any] = "bias" else: lowerCAmelCase__ : Dict = None set_recursively(__snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) continue if not is_used: unused_weights.append(__snake_case ) logger.warning(F'''Unused weights: {unused_weights}''' ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: lowerCAmelCase__ : Tuple = full_name.split('conv_layers.' )[-1] lowerCAmelCase__ : str = name.split('.' ) lowerCAmelCase__ : Any = int(items[0] ) lowerCAmelCase__ : Tuple = 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.''' ) lowerCAmelCase__ : Union[str, Any] = 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.''' ) lowerCAmelCase__ : List[str] = 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." ) lowerCAmelCase__ : int = 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.''' ) lowerCAmelCase__ : Optional[int] = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(__snake_case ) @torch.no_grad() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=True ) -> Dict: if config_path is not None: lowerCAmelCase__ : Optional[Any] = HubertConfig.from_pretrained(__snake_case ) else: lowerCAmelCase__ : Dict = HubertConfig() if is_finetuned: if dict_path: lowerCAmelCase__ : Dict = Dictionary.load(__snake_case ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq lowerCAmelCase__ : Union[str, Any] = target_dict.pad_index lowerCAmelCase__ : Dict = target_dict.bos_index lowerCAmelCase__ : List[Any] = target_dict.eos_index lowerCAmelCase__ : List[str] = len(target_dict.symbols ) lowerCAmelCase__ : Any = os.path.join(__snake_case , 'vocab.json' ) if not os.path.isdir(__snake_case ): logger.error('--pytorch_dump_folder_path ({}) should be a directory'.format(__snake_case ) ) return os.makedirs(__snake_case , exist_ok=__snake_case ) with open(__snake_case , 'w' , encoding='utf-8' ) as vocab_handle: json.dump(target_dict.indices , __snake_case ) lowerCAmelCase__ : List[str] = WavaVecaCTCTokenizer( __snake_case , 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=__snake_case , ) lowerCAmelCase__ : str = True if config.feat_extract_norm == "layer" else False lowerCAmelCase__ : int = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16_000 , padding_value=0 , do_normalize=__snake_case , return_attention_mask=__snake_case , ) lowerCAmelCase__ : str = WavaVecaProcessor(feature_extractor=__snake_case , tokenizer=__snake_case ) processor.save_pretrained(__snake_case ) lowerCAmelCase__ : List[str] = HubertForCTC(__snake_case ) else: lowerCAmelCase__ : str = HubertModel(__snake_case ) if is_finetuned: lowerCAmelCase__ : int = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'data': '/'.join(dict_path.split('/' )[:-1] )} ) else: lowerCAmelCase__ : int = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) lowerCAmelCase__ : Optional[int] = model[0].eval() recursively_load_weights(__snake_case , __snake_case , __snake_case ) hf_wavavec.save_pretrained(__snake_case ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""") parser.add_argument("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument( """--not_finetuned""", action="""store_true""", help="""Whether the model to convert is a fine-tuned model or not""" ) lowerCamelCase__ = parser.parse_args() convert_hubert_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
364
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: stooge(SCREAMING_SNAKE_CASE_ , 0 , len(SCREAMING_SNAKE_CASE_ ) - 1 ) return arr def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowerCAmelCase__ , lowerCAmelCase__ : Tuple = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowerCAmelCase__ : Union[str, Any] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) # Recursively sort last 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , i + t , (SCREAMING_SNAKE_CASE_) ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) if __name__ == "__main__": lowerCamelCase__ = input("""Enter numbers separated by a comma:\n""").strip() lowerCamelCase__ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = 50 ) -> List[Any]: lowerCAmelCase__ : Dict = [1] * (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 ): ways_number[row_length] += ways_number[ row_length - tile_start - tile_length ] return ways_number[length] if __name__ == "__main__": print(F"""{solution() = }""")
365
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) lowerCamelCase__ = { """configuration_speech_to_text""": ["""SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Speech2TextConfig"""], """processing_speech_to_text""": ["""Speech2TextProcessor"""], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextTokenizer"""] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextFeatureExtractor"""] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFSpeech2TextForConditionalGeneration""", """TFSpeech2TextModel""", """TFSpeech2TextPreTrainedModel""", ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """Speech2TextForConditionalGeneration""", """Speech2TextModel""", """Speech2TextPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
import json import os import tempfile import unittest import numpy as np from datasets import load_dataset from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ImageGPTImageProcessor class A__ ( unittest.TestCase ): def __init__( self : Tuple , a : Tuple , a : int=7 , a : Union[str, Any]=3 , a : Any=18 , a : Optional[Any]=30 , a : List[str]=400 , a : Union[str, Any]=True , a : Optional[int]=None , a : List[str]=True , ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = size if size is not None else {'height': 18, 'width': 18} lowerCAmelCase__ : int = parent lowerCAmelCase__ : Union[str, Any] = batch_size lowerCAmelCase__ : int = num_channels lowerCAmelCase__ : List[str] = image_size lowerCAmelCase__ : List[Any] = min_resolution lowerCAmelCase__ : int = max_resolution lowerCAmelCase__ : Optional[int] = do_resize lowerCAmelCase__ : Tuple = size lowerCAmelCase__ : List[Any] = do_normalize def _lowerCamelCase ( self : Any ): '''simple docstring''' return { # here we create 2 clusters for the sake of simplicity "clusters": np.asarray( [ [0.8_8_6_6_4_4_3_6_3_4_0_3_3_2_0_3, 0.6_6_1_8_8_2_9_3_6_9_5_4_4_9_8_3, 0.3_8_9_1_7_4_6_4_0_1_7_8_6_8_0_4], [-0.6_0_4_2_5_5_9_1_4_6_8_8_1_1_0_4, -0.0_2_2_9_5_0_0_8_8_6_0_5_2_8_4_6_9, 0.5_4_2_3_7_9_7_3_6_9_0_0_3_2_9_6], ] ), "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, } @require_torch @require_vision class A__ ( __magic_name__ , unittest.TestCase ): lowercase = ImageGPTImageProcessor if is_vision_available() else None def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = ImageGPTImageProcessingTester(self ) @property def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_lowercase , 'clusters' ) ) self.assertTrue(hasattr(_lowercase , 'do_resize' ) ) self.assertTrue(hasattr(_lowercase , 'size' ) ) self.assertTrue(hasattr(_lowercase , 'do_normalize' ) ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : str = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 18} ) lowerCAmelCase__ : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.image_processing_class(**self.image_processor_dict ) lowerCAmelCase__ : Dict = json.loads(image_processor.to_json_string() ) for key, value in self.image_processor_dict.items(): if key == "clusters": self.assertTrue(np.array_equal(_lowercase , obj[key] ) ) else: self.assertEqual(obj[key] , _lowercase ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[Any] = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: lowerCAmelCase__ : str = os.path.join(_lowercase , 'image_processor.json' ) image_processor_first.to_json_file(_lowercase ) lowerCAmelCase__ : int = self.image_processing_class.from_json_file(_lowercase ).to_dict() lowerCAmelCase__ : Optional[int] = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(_lowercase , image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] , _lowercase ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: image_processor_first.save_pretrained(_lowercase ) lowerCAmelCase__ : Dict = self.image_processing_class.from_pretrained(_lowercase ).to_dict() lowerCAmelCase__ : Tuple = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(_lowercase , image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] , _lowercase ) @unittest.skip('ImageGPT requires clusters at initialization' ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' pass def lowerCAmelCase__ ( ) -> int: lowerCAmelCase__ : List[str] = load_dataset('hf-internal-testing/fixtures_image_utils' , split='test' ) lowerCAmelCase__ : Union[str, Any] = Image.open(dataset[4]['file'] ) lowerCAmelCase__ : Union[str, Any] = Image.open(dataset[5]['file'] ) lowerCAmelCase__ : List[Any] = [imagea, imagea] return images @require_vision @require_torch class A__ ( unittest.TestCase ): @slow def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Any = ImageGPTImageProcessor.from_pretrained('openai/imagegpt-small' ) lowerCAmelCase__ : Optional[Any] = prepare_images() # test non-batched lowerCAmelCase__ : List[Any] = image_processing(images[0] , return_tensors='pt' ) self.assertIsInstance(encoding.input_ids , torch.LongTensor ) self.assertEqual(encoding.input_ids.shape , (1, 1_024) ) lowerCAmelCase__ : Optional[Any] = [306, 191, 191] self.assertEqual(encoding.input_ids[0, :3].tolist() , _lowercase ) # test batched lowerCAmelCase__ : Union[str, Any] = image_processing(_lowercase , return_tensors='pt' ) self.assertIsInstance(encoding.input_ids , torch.LongTensor ) self.assertEqual(encoding.input_ids.shape , (2, 1_024) ) lowerCAmelCase__ : Any = [303, 13, 13] self.assertEqual(encoding.input_ids[1, -3:].tolist() , _lowercase )
366
import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = { 'task_specific_params': { 'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4}, 'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4}, 'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6}, } } lowerCAmelCase__ : int = { 'task_specific_params.summarization.length_penalty': 1.0, 'task_specific_params.summarization.max_length': 128, 'task_specific_params.summarization.min_length': 12, 'task_specific_params.summarization.num_beams': 4, 'task_specific_params.summarization_cnn.length_penalty': 2.0, 'task_specific_params.summarization_cnn.max_length': 142, 'task_specific_params.summarization_cnn.min_length': 56, 'task_specific_params.summarization_cnn.num_beams': 4, 'task_specific_params.summarization_xsum.length_penalty': 1.0, 'task_specific_params.summarization_xsum.max_length': 62, 'task_specific_params.summarization_xsum.min_length': 11, 'task_specific_params.summarization_xsum.num_beams': 6, } self.assertEqual(flatten_dict(a ) , a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(a ) , x.transpose() ) ) lowerCAmelCase__ : List[str] = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : int = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Dict = tf.constant(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : int = jnp.array(a ) self.assertTrue(np.allclose(transpose(a ) , np.asarray(transpose(a ) ) ) ) lowerCAmelCase__ : Any = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = jnp.array(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , np.asarray(transpose(a , axes=(1, 2, 0) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.reshape(a , (4, 3) ) ) ) lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.reshape(a , (12, 5) ) ) ) @require_torch def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : Dict = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[str] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.asarray(reshape(a , (4, 3) ) ) ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.asarray(reshape(a , (12, 5) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(a ) , np.squeeze(a ) ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.squeeze(a , axis=2 ) ) ) @require_torch def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : Optional[Any] = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : str = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a ) , np.asarray(squeeze(a ) ) ) ) lowerCAmelCase__ : str = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Optional[Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.asarray(squeeze(a , axis=2 ) ) ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.expand_dims(a , axis=1 ) ) ) @require_torch def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : str = np.random.randn(3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = np.random.randn(3 , 4 ) lowerCAmelCase__ : Tuple = jnp.array(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.asarray(expand_dims(a , axis=1 ) ) ) )
307
0
import inspect import unittest from transformers import RegNetConfig, is_flax_available from transformers.testing_utils import require_flax, slow from transformers.utils import cached_property, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class A__ ( unittest.TestCase ): def __init__( self : Union[str, Any] , a : Dict , a : str=3 , a : Any=32 , a : str=3 , a : str=10 , a : Tuple=[10, 20, 30, 40] , a : Any=[1, 1, 2, 1] , a : Any=True , a : Any=True , a : Optional[Any]="relu" , a : Union[str, Any]=3 , a : str=None , ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = parent lowerCAmelCase__ : List[str] = batch_size lowerCAmelCase__ : List[Any] = image_size lowerCAmelCase__ : Optional[int] = num_channels lowerCAmelCase__ : str = embeddings_size lowerCAmelCase__ : str = hidden_sizes lowerCAmelCase__ : List[str] = depths lowerCAmelCase__ : Optional[Any] = is_training lowerCAmelCase__ : int = use_labels lowerCAmelCase__ : Union[str, Any] = hidden_act lowerCAmelCase__ : int = num_labels lowerCAmelCase__ : int = scope lowerCAmelCase__ : str = len(_UpperCAmelCase ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase__ : Dict = self.get_config() return config, pixel_values def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' 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 , image_size=self.image_size , ) def _lowerCamelCase ( self : str , a : Optional[int] , a : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = FlaxRegNetModel(config=_UpperCAmelCase ) lowerCAmelCase__ : Union[str, Any] = model(_UpperCAmelCase ) # Output shape (b, c, h, w) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def _lowerCamelCase ( self : int , a : List[Any] , a : Dict ): '''simple docstring''' lowerCAmelCase__ : List[Any] = self.num_labels lowerCAmelCase__ : List[str] = FlaxRegNetForImageClassification(config=_UpperCAmelCase ) lowerCAmelCase__ : str = model(_UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[Any] = self.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = config_and_inputs lowerCAmelCase__ : List[str] = {'pixel_values': pixel_values} return config, inputs_dict @require_flax class A__ ( lowerCamelCase_ , unittest.TestCase ): lowercase = (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else () lowercase = False lowercase = False lowercase = False def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = FlaxRegNetModelTester(self ) lowerCAmelCase__ : Dict = ConfigTester(self , config_class=_UpperCAmelCase , has_text_modality=_UpperCAmelCase ) def _lowerCamelCase ( self : Any ): '''simple docstring''' self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCAmelCase ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase ) @unittest.skip(reason='RegNet does not use inputs_embeds' ) def _lowerCamelCase ( self : Any ): '''simple docstring''' pass @unittest.skip(reason='RegNet does not support input and output embeddings' ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase__ : Union[str, Any] = model_class(_UpperCAmelCase ) lowerCAmelCase__ : Optional[int] = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase__ : Union[str, Any] = [*signature.parameters.keys()] lowerCAmelCase__ : Tuple = ['pixel_values'] self.assertListEqual(arg_names[:1] , _UpperCAmelCase ) def _lowerCamelCase ( self : str ): '''simple docstring''' def check_hidden_states_output(a : Any , a : Optional[Any] , a : Dict ): lowerCAmelCase__ : Union[str, Any] = model_class(_UpperCAmelCase ) lowerCAmelCase__ : Union[str, Any] = model(**self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) ) lowerCAmelCase__ : List[str] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowerCAmelCase__ : int = self.model_tester.num_stages self.assertEqual(len(_UpperCAmelCase ) , expected_num_stages + 1 ) lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase__ : int = True check_hidden_states_output(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCAmelCase__ : Dict = True check_hidden_states_output(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): lowerCAmelCase__ : Optional[Any] = self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase__ : str = model_class(_UpperCAmelCase ) @jax.jit def model_jitted(a : Optional[int] , **a : Dict ): return model(pixel_values=_UpperCAmelCase , **_UpperCAmelCase ) with self.subTest('JIT Enabled' ): lowerCAmelCase__ : Optional[int] = model_jitted(**_UpperCAmelCase ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): lowerCAmelCase__ : Optional[int] = model_jitted(**_UpperCAmelCase ).to_tuple() self.assertEqual(len(_UpperCAmelCase ) , len(_UpperCAmelCase ) ) for jitted_output, output in zip(_UpperCAmelCase , _UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def lowerCAmelCase__ ( ) -> Optional[Any]: lowerCAmelCase__ : Any = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_flax class A__ ( unittest.TestCase ): @cached_property def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return AutoImageProcessor.from_pretrained('facebook/regnet-y-040' ) if is_vision_available() else None @slow def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Any = FlaxRegNetForImageClassification.from_pretrained('facebook/regnet-y-040' ) lowerCAmelCase__ : int = self.default_image_processor lowerCAmelCase__ : Optional[Any] = prepare_img() lowerCAmelCase__ : List[str] = image_processor(images=_UpperCAmelCase , return_tensors='np' ) lowerCAmelCase__ : List[str] = model(**_UpperCAmelCase ) # verify the logits lowerCAmelCase__ : Optional[Any] = (1, 1_000) self.assertEqual(outputs.logits.shape , _UpperCAmelCase ) lowerCAmelCase__ : List[str] = jnp.array([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6] ) self.assertTrue(jnp.allclose(outputs.logits[0, :3] , _UpperCAmelCase , atol=1E-4 ) )
367
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position lowerCamelCase__ = """2.13.1""" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("""3.7"""): raise ImportWarning( """To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition.""" ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( """To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n""" """If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.""" ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip lowerCamelCase__ = concatenate_datasets lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadManager lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
307
0
import functools from typing import Any def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> bool: if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or len(SCREAMING_SNAKE_CASE_ ) == 0: raise ValueError('the string should be not empty string' ) if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or not all( isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) and len(SCREAMING_SNAKE_CASE_ ) > 0 for item in words ): raise ValueError('the words should be a list of non-empty strings' ) # Build trie lowerCAmelCase__ : dict[str, Any] = {} lowerCAmelCase__ : Tuple = """WORD_KEEPER""" for word in words: lowerCAmelCase__ : List[str] = trie for c in word: if c not in trie_node: lowerCAmelCase__ : Tuple = {} lowerCAmelCase__ : Dict = trie_node[c] lowerCAmelCase__ : Any = True lowerCAmelCase__ : str = len(SCREAMING_SNAKE_CASE_ ) # Dynamic programming method @functools.cache def is_breakable(SCREAMING_SNAKE_CASE_ ) -> bool: if index == len_string: return True lowerCAmelCase__ : Optional[int] = trie for i in range(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Tuple = trie_node.get(string[i] , SCREAMING_SNAKE_CASE_ ) if trie_node is None: return False if trie_node.get(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
368
import gc import unittest import numpy as np import torch from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = DanceDiffusionPipeline lowercase = UNCONDITIONAL_AUDIO_GENERATION_PARAMS lowercase = PipelineTesterMixin.required_optional_params - { 'callback', 'latents', 'callback_steps', 'output_type', 'num_images_per_prompt', } lowercase = UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS lowercase = False lowercase = False def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = UNetaDModel( block_out_channels=(32, 32, 64) , extra_in_channels=16 , sample_size=512 , sample_rate=16_000 , in_channels=2 , out_channels=2 , flip_sin_to_cos=a , use_timestep_embedding=a , time_embedding_type='fourier' , mid_block_type='UNetMidBlock1D' , down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D') , up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip') , ) lowerCAmelCase__ : Tuple = IPNDMScheduler() lowerCAmelCase__ : str = { 'unet': unet, 'scheduler': scheduler, } return components def _lowerCamelCase ( self : int , a : Dict , a : List[str]=0 ): '''simple docstring''' if str(a ).startswith('mps' ): lowerCAmelCase__ : Union[str, Any] = torch.manual_seed(a ) else: lowerCAmelCase__ : Optional[Any] = torch.Generator(device=a ).manual_seed(a ) lowerCAmelCase__ : Optional[Any] = { 'batch_size': 1, 'generator': generator, 'num_inference_steps': 4, } return inputs def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'cpu' # ensure determinism for the device-dependent torch.Generator lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : List[str] = DanceDiffusionPipeline(**a ) lowerCAmelCase__ : Any = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a ) lowerCAmelCase__ : List[Any] = pipe(**a ) lowerCAmelCase__ : List[str] = output.audios lowerCAmelCase__ : Optional[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, components["unet"].sample_size) lowerCAmelCase__ : List[Any] = np.array([-0.7_2_6_5, 1.0_0_0_0, -0.8_3_8_8, 0.1_1_7_5, 0.9_4_9_8, -1.0_0_0_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) @skip_mps def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return super().test_save_load_optional_components() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_attention_slicing_forward_pass() def _lowerCamelCase ( self : List[str] ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Dict ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device lowerCAmelCase__ : List[str] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' ) lowerCAmelCase__ : List[str] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : int = output.audios lowerCAmelCase__ : List[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : Dict = np.array([-0.0_1_9_2, -0.0_2_3_1, -0.0_3_1_8, -0.0_0_5_9, 0.0_0_0_2, -0.0_0_2_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = torch_device lowerCAmelCase__ : List[Any] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Optional[int] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : str = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : str = output.audios lowerCAmelCase__ : Tuple = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : int = np.array([-0.0_3_6_7, -0.0_4_8_8, -0.0_7_7_1, -0.0_5_2_5, -0.0_4_4_4, -0.0_3_4_1] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2
307
0
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class A__ ( __magic_name__ ): lowercase = ['image_processor', 'tokenizer'] lowercase = 'OwlViTImageProcessor' lowercase = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : List[str] , a : List[str]=None , a : int=None , **a : Tuple ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , lowercase_ , ) lowerCAmelCase__ : Optional[int] = kwargs.pop('feature_extractor' ) lowerCAmelCase__ : Optional[int] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(lowercase_ , lowercase_ ) def __call__( self : Union[str, Any] , a : Dict=None , a : Tuple=None , a : str=None , a : Optional[int]="max_length" , a : Union[str, Any]="np" , **a : List[Any] ): '''simple docstring''' if text is None and query_images is None and images is None: raise ValueError( 'You have to specify at least one text or query image or image. All three cannot be none.' ) if text is not None: if isinstance(lowercase_ , lowercase_ ) or (isinstance(lowercase_ , lowercase_ ) and not isinstance(text[0] , lowercase_ )): lowerCAmelCase__ : List[Any] = [self.tokenizer(lowercase_ , padding=lowercase_ , return_tensors=lowercase_ , **lowercase_ )] elif isinstance(lowercase_ , lowercase_ ) and isinstance(text[0] , lowercase_ ): lowerCAmelCase__ : List[Any] = [] # Maximum number of queries across batch lowerCAmelCase__ : Union[str, Any] = max([len(lowercase_ ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(lowercase_ ) != max_num_queries: lowerCAmelCase__ : Optional[int] = t + [' '] * (max_num_queries - len(lowercase_ )) lowerCAmelCase__ : Union[str, Any] = self.tokenizer(lowercase_ , padding=lowercase_ , return_tensors=lowercase_ , **lowercase_ ) encodings.append(lowercase_ ) else: raise TypeError('Input text should be a string, a list of strings or a nested list of strings' ) if return_tensors == "np": lowerCAmelCase__ : List[Any] = np.concatenate([encoding['input_ids'] for encoding in encodings] , axis=0 ) lowerCAmelCase__ : List[str] = np.concatenate([encoding['attention_mask'] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp lowerCAmelCase__ : Tuple = jnp.concatenate([encoding['input_ids'] for encoding in encodings] , axis=0 ) lowerCAmelCase__ : Tuple = jnp.concatenate([encoding['attention_mask'] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch lowerCAmelCase__ : List[str] = torch.cat([encoding['input_ids'] for encoding in encodings] , dim=0 ) lowerCAmelCase__ : Any = torch.cat([encoding['attention_mask'] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf lowerCAmelCase__ : int = tf.stack([encoding['input_ids'] for encoding in encodings] , axis=0 ) lowerCAmelCase__ : List[Any] = tf.stack([encoding['attention_mask'] for encoding in encodings] , axis=0 ) else: raise ValueError('Target return tensor type could not be returned' ) lowerCAmelCase__ : List[str] = BatchEncoding() lowerCAmelCase__ : Any = input_ids lowerCAmelCase__ : int = attention_mask if query_images is not None: lowerCAmelCase__ : Optional[Any] = BatchEncoding() lowerCAmelCase__ : Optional[Any] = self.image_processor( lowercase_ , return_tensors=lowercase_ , **lowercase_ ).pixel_values lowerCAmelCase__ : Union[str, Any] = query_pixel_values if images is not None: lowerCAmelCase__ : List[Any] = self.image_processor(lowercase_ , return_tensors=lowercase_ , **lowercase_ ) if text is not None and images is not None: lowerCAmelCase__ : List[Any] = image_features.pixel_values return encoding elif query_images is not None and images is not None: lowerCAmelCase__ : Tuple = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**lowercase_ ) , tensor_type=lowercase_ ) def _lowerCamelCase ( self : List[Any] , *a : str , **a : int ): '''simple docstring''' return self.image_processor.post_process(*lowercase_ , **lowercase_ ) def _lowerCamelCase ( self : Dict , *a : str , **a : List[str] ): '''simple docstring''' return self.image_processor.post_process_object_detection(*lowercase_ , **lowercase_ ) def _lowerCamelCase ( self : Union[str, Any] , *a : Dict , **a : Dict ): '''simple docstring''' return self.image_processor.post_process_image_guided_detection(*lowercase_ , **lowercase_ ) def _lowerCamelCase ( self : str , *a : int , **a : List[str] ): '''simple docstring''' return self.tokenizer.batch_decode(*lowercase_ , **lowercase_ ) def _lowerCamelCase ( self : List[str] , *a : Optional[int] , **a : Optional[int] ): '''simple docstring''' return self.tokenizer.decode(*lowercase_ , **lowercase_ ) @property def _lowerCamelCase ( self : Dict ): '''simple docstring''' warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' , lowercase_ , ) return self.image_processor_class @property def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' , lowercase_ , ) return self.image_processor
369
from ..utils import DummyObject, requires_backends class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Union[str, Any] , *a : Optional[int] , **a : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : List[Any] , **a : int ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Any , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Optional[Any] , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Optional[int] , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : List[Any] , *a : List[str] , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Union[str, Any] , **a : Optional[int] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : List[Any] , *a : Dict , **a : List[str] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Dict , **a : List[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Dict , *a : str , **a : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Union[str, Any] , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : Union[str, Any] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Tuple , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] )
307
0
import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """post_extract_proj""": """feature_projection.projection""", """encoder.pos_conv.0""": """encoder.pos_conv_embed.conv""", """self_attn.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.layer_norm""": """encoder.layer_norm""", """w2v_model.layer_norm""": """feature_projection.layer_norm""", """quantizer.weight_proj""": """quantizer.weight_proj""", """quantizer.vars""": """quantizer.codevectors""", """project_q""": """project_q""", """final_proj""": """project_hid""", """w2v_encoder.proj""": """lm_head""", """mask_emb""": """masked_spec_embed""", } lowerCamelCase__ = [ """lm_head""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", ] def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: for attribute in key.split('.' ): lowerCAmelCase__ : Any = getattr(_lowerCamelCase , _lowerCamelCase ) if weight_type is not None: lowerCAmelCase__ : List[str] = getattr(_lowerCamelCase , _lowerCamelCase ).shape else: lowerCAmelCase__ : Union[str, Any] = 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": lowerCAmelCase__ : List[Any] = value elif weight_type == "weight_g": lowerCAmelCase__ : Union[str, Any] = value elif weight_type == "weight_v": lowerCAmelCase__ : Tuple = value elif weight_type == "bias": lowerCAmelCase__ : int = value else: lowerCAmelCase__ : Union[str, Any] = value logger.info(F'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: lowerCAmelCase__ : int = [] lowerCAmelCase__ : Tuple = fairseq_model.state_dict() lowerCAmelCase__ : Tuple = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight lowerCAmelCase__ : Any = None for name, value in fairseq_dict.items(): lowerCAmelCase__ : Optional[Any] = False if "conv_layers" in name: load_conv_layer( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , hf_model.config.feat_extract_norm == 'group' , ) lowerCAmelCase__ : Optional[Any] = True elif name.split('.' )[0] == "proj": lowerCAmelCase__ : int = fairseq_model.proj lowerCAmelCase__ : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: lowerCAmelCase__ : int = True if "*" in mapped_key: lowerCAmelCase__ : Dict = name.split(_lowerCamelCase )[0].split('.' )[-2] lowerCAmelCase__ : Dict = mapped_key.replace('*' , _lowerCamelCase ) if "weight_g" in name: lowerCAmelCase__ : Optional[Any] = "weight_g" elif "weight_v" in name: lowerCAmelCase__ : Tuple = "weight_v" elif "bias" in name: lowerCAmelCase__ : List[str] = "bias" elif "weight" in name: lowerCAmelCase__ : Dict = "weight" else: lowerCAmelCase__ : List[Any] = None set_recursively(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) continue if not is_used: unused_weights.append(_lowerCamelCase ) logger.warning(F'''Unused weights: {unused_weights}''' ) return proj_weight def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: lowerCAmelCase__ : List[str] = full_name.split('conv_layers.' )[-1] lowerCAmelCase__ : List[str] = name.split('.' ) lowerCAmelCase__ : int = int(items[0] ) lowerCAmelCase__ : Any = 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.''' ) lowerCAmelCase__ : Dict = 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.''' ) lowerCAmelCase__ : Union[str, Any] = 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." ) lowerCAmelCase__ : Union[str, Any] = 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.''' ) lowerCAmelCase__ : int = 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__ ( SCREAMING_SNAKE_CASE_ ) -> Any: lowerCAmelCase__ : Optional[Any] = emb.weight.shape lowerCAmelCase__ : List[Any] = nn.Linear(_lowerCamelCase , _lowerCamelCase , bias=_lowerCamelCase ) lowerCAmelCase__ : Optional[int] = emb.weight.data return lin_layer def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: with open(_lowerCamelCase , 'r' , encoding='utf-8' ) as f: lowerCAmelCase__ : str = f.readlines() lowerCAmelCase__ : int = [line.split(' ' )[0] for line in lines] lowerCAmelCase__ : Optional[int] = len(_lowerCamelCase ) lowerCAmelCase__ : Union[str, Any] = { "<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3, } vocab_dict.update(dict(zip(_lowerCamelCase , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) -> Dict: lowerCAmelCase__ : str = WavaVecaConfig.from_pretrained(_lowerCamelCase ) lowerCAmelCase__ : Any = SpeechaTextaConfig.from_pretrained( _lowerCamelCase , vocab_size=_lowerCamelCase , decoder_layers=_lowerCamelCase , do_stable_layer_norm=_lowerCamelCase ) lowerCAmelCase__ : str = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16_000 , padding_value=0 , do_normalize=_lowerCamelCase , return_attention_mask=_lowerCamelCase , ) lowerCAmelCase__ : Tuple = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'data': '/'.join(dict_path.split('/' )[:-1] )} ) lowerCAmelCase__ : Tuple = model[0].eval() # set weights for wav2vec2 encoder lowerCAmelCase__ : str = WavaVecaModel(_lowerCamelCase ) lowerCAmelCase__ : List[str] = recursively_load_weights_wavaveca(model.encoder , _lowerCamelCase ) lowerCAmelCase__ : int = SpeechaTextaForCausalLM(_lowerCamelCase ) lowerCAmelCase__ : Optional[int] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_lowerCamelCase ) # set output linear layer unexpected_keys.remove('embed_out' ) lowerCAmelCase__ : Union[str, Any] = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(F'''The following keys are missing when loading the decoder weights: {missing_keys}''' ) logger.warning(F'''The following keys are unexpected when loading the decoder weights: {unexpected_keys}''' ) lowerCAmelCase__ : int = SpeechEncoderDecoderModel(encoder=_lowerCamelCase , decoder=_lowerCamelCase ) lowerCAmelCase__ : List[str] = False # add projection layer lowerCAmelCase__ : str = nn.Parameter(projection_layer.weight ) lowerCAmelCase__ : int = nn.Parameter(projection_layer.bias ) lowerCAmelCase__ : int = create_vocab_dict(_lowerCamelCase ) with open(os.path.join(_lowerCamelCase , 'vocab.json' ) , 'w' ) as fp: json.dump(_lowerCamelCase , _lowerCamelCase ) lowerCAmelCase__ : str = SpeechaTextaTokenizer(os.path.join(_lowerCamelCase , 'vocab.json' ) ) tokenizer.save_pretrained(_lowerCamelCase ) lowerCAmelCase__ : Tuple = hf_wavavec.config.to_dict() lowerCAmelCase__ : Any = tokenizer.pad_token_id lowerCAmelCase__ : List[Any] = tokenizer.bos_token_id lowerCAmelCase__ : List[str] = tokenizer.eos_token_id lowerCAmelCase__ : Dict = "speech_to_text_2" lowerCAmelCase__ : List[str] = "wav2vec2" lowerCAmelCase__ : Tuple = SpeechEncoderDecoderConfig.from_dict(_lowerCamelCase ) hf_wavavec.save_pretrained(_lowerCamelCase ) feature_extractor.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""") parser.add_argument("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""") parser.add_argument( """--encoder_config_path""", default="""facebook/wav2vec2-large-lv60""", type=str, help="""Path to hf encoder wav2vec2 checkpoint config""", ) parser.add_argument( """--decoder_config_path""", default="""facebook/s2t-small-mustc-en-fr-st""", type=str, help="""Path to hf decoder s2t checkpoint config""", ) parser.add_argument("""--vocab_size""", default=1_0224, type=int, help="""Vocab size of decoder""") parser.add_argument("""--num_decoder_layers""", default=7, type=int, help="""Number of decoder layers""") lowerCamelCase__ = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
370
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class A__ : def __init__( self : List[str] , a : Any , a : Dict=13 , a : Optional[Any]=7 , a : Tuple=True , a : Tuple=True , a : Dict=False , a : Optional[Any]=True , a : Dict=99 , a : Tuple=32 , a : Optional[Any]=5 , a : str=4 , a : Union[str, Any]=37 , a : Any="gelu" , a : Dict=0.1 , a : Any=0.1 , a : Optional[int]=512 , a : Union[str, Any]=16 , a : Optional[int]=2 , a : Optional[Any]=0.0_2 , a : List[Any]=3 , a : Any=4 , a : Optional[int]=None , ): '''simple docstring''' lowerCAmelCase__ : List[str] = parent lowerCAmelCase__ : str = batch_size lowerCAmelCase__ : Optional[int] = seq_length lowerCAmelCase__ : Optional[Any] = is_training lowerCAmelCase__ : Tuple = use_input_mask lowerCAmelCase__ : List[Any] = use_token_type_ids lowerCAmelCase__ : str = use_labels lowerCAmelCase__ : Dict = vocab_size lowerCAmelCase__ : Union[str, Any] = hidden_size lowerCAmelCase__ : Optional[int] = num_hidden_layers lowerCAmelCase__ : List[Any] = num_attention_heads lowerCAmelCase__ : int = intermediate_size lowerCAmelCase__ : Union[str, Any] = hidden_act lowerCAmelCase__ : Union[str, Any] = hidden_dropout_prob lowerCAmelCase__ : Any = attention_probs_dropout_prob lowerCAmelCase__ : Dict = max_position_embeddings lowerCAmelCase__ : int = type_vocab_size lowerCAmelCase__ : int = type_sequence_label_size lowerCAmelCase__ : Dict = initializer_range lowerCAmelCase__ : List[str] = num_labels lowerCAmelCase__ : Any = num_choices lowerCAmelCase__ : List[Any] = scope def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase__ : Tuple = None if self.use_input_mask: lowerCAmelCase__ : str = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase__ : List[str] = None if self.use_token_type_ids: lowerCAmelCase__ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCAmelCase__ : Optional[Any] = None lowerCAmelCase__ : Dict = None lowerCAmelCase__ : str = None if self.use_labels: lowerCAmelCase__ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCAmelCase__ : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) lowerCAmelCase__ : List[str] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return LlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=a , initializer_range=self.initializer_range , ) def _lowerCamelCase ( self : Tuple , a : Dict , a : List[str] , a : str , a : Union[str, Any] , a : Optional[Any] , a : Dict , a : str ): '''simple docstring''' lowerCAmelCase__ : str = LlamaModel(config=a ) model.to(a ) model.eval() lowerCAmelCase__ : Dict = model(a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCamelCase ( self : int , a : Any , a : Union[str, Any] , a : Dict , a : Dict , a : List[Any] , a : Optional[Any] , a : int , a : Dict , a : Tuple , ): '''simple docstring''' lowerCAmelCase__ : int = True lowerCAmelCase__ : Dict = LlamaModel(a ) model.to(a ) model.eval() lowerCAmelCase__ : List[Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , ) lowerCAmelCase__ : Optional[int] = model( a , attention_mask=a , encoder_hidden_states=a , ) lowerCAmelCase__ : Union[str, Any] = model(a , attention_mask=a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCamelCase ( self : Union[str, Any] , a : int , a : List[Any] , a : int , a : Tuple , a : List[Any] , a : Union[str, Any] , a : Any , a : List[str] , a : List[str] , ): '''simple docstring''' lowerCAmelCase__ : List[Any] = LlamaForCausalLM(config=a ) model.to(a ) model.eval() lowerCAmelCase__ : Tuple = model(a , attention_mask=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowerCamelCase ( self : str , a : Any , a : Tuple , a : str , a : Union[str, Any] , a : Optional[Any] , a : List[Any] , a : Optional[Any] , a : Optional[Any] , a : List[str] , ): '''simple docstring''' lowerCAmelCase__ : str = True lowerCAmelCase__ : Optional[int] = True lowerCAmelCase__ : List[Any] = LlamaForCausalLM(config=a ) model.to(a ) model.eval() # first forward pass lowerCAmelCase__ : List[str] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , use_cache=a , ) lowerCAmelCase__ : List[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids lowerCAmelCase__ : Union[str, Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowerCAmelCase__ : Tuple = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and lowerCAmelCase__ : int = torch.cat([input_ids, next_tokens] , dim=-1 ) lowerCAmelCase__ : Any = torch.cat([input_mask, next_mask] , dim=-1 ) lowerCAmelCase__ : Union[str, Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , output_hidden_states=a , )['hidden_states'][0] lowerCAmelCase__ : Union[str, Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , past_key_values=a , output_hidden_states=a , )['hidden_states'][0] # select random slice lowerCAmelCase__ : Any = ids_tensor((1,) , output_from_past.shape[-1] ).item() lowerCAmelCase__ : Optional[int] = output_from_no_past[:, -3:, random_slice_idx].detach() lowerCAmelCase__ : Any = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(a , a , atol=1E-3 ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.prepare_config_and_inputs() ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : Any = config_and_inputs lowerCAmelCase__ : List[str] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class A__ ( __magic_name__ , __magic_name__ , __magic_name__ , unittest.TestCase ): lowercase = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () lowercase = (LlamaForCausalLM,) if is_torch_available() else () lowercase = ( { 'feature-extraction': LlamaModel, 'text-classification': LlamaForSequenceClassification, 'text-generation': LlamaForCausalLM, 'zero-shot': LlamaForSequenceClassification, } if is_torch_available() else {} ) lowercase = False lowercase = False def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Tuple = LlamaModelTester(self ) lowerCAmelCase__ : str = ConfigTester(self , config_class=a , hidden_size=37 ) def _lowerCamelCase ( self : str ): '''simple docstring''' self.config_tester.run_common_tests() def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCAmelCase__ : int = type self.model_tester.create_and_check_model(*a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : int = 3 lowerCAmelCase__ : Dict = input_dict['input_ids'] lowerCAmelCase__ : Optional[Any] = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Tuple = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : Tuple = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : str = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : List[Any] = 3 lowerCAmelCase__ : List[str] = 'single_label_classification' lowerCAmelCase__ : List[Any] = input_dict['input_ids'] lowerCAmelCase__ : List[Any] = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Optional[int] = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : int = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : Optional[int] = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Optional[Any] = 3 lowerCAmelCase__ : Optional[Any] = 'multi_label_classification' lowerCAmelCase__ : List[str] = input_dict['input_ids'] lowerCAmelCase__ : Tuple = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Any = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) lowerCAmelCase__ : Dict = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : Dict = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' pass @parameterized.expand([('linear',), ('dynamic',)] ) def _lowerCamelCase ( self : Optional[int] , a : Dict ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Tuple = ids_tensor([1, 10] , config.vocab_size ) lowerCAmelCase__ : List[str] = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : List[Any] = LlamaModel(a ) original_model.to(a ) original_model.eval() lowerCAmelCase__ : List[Any] = original_model(a ).last_hidden_state lowerCAmelCase__ : str = original_model(a ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : Any = {'type': scaling_type, 'factor': 1_0.0} lowerCAmelCase__ : Union[str, Any] = LlamaModel(a ) scaled_model.to(a ) scaled_model.eval() lowerCAmelCase__ : Union[str, Any] = scaled_model(a ).last_hidden_state lowerCAmelCase__ : Optional[int] = scaled_model(a ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(a , a , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(a , a , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(a , a , atol=1E-5 ) ) @require_torch class A__ ( unittest.TestCase ): @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' , device_map='auto' ) lowerCAmelCase__ : Any = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 lowerCAmelCase__ : Dict = torch.tensor([[-6.6_5_5_0, -4.1_2_2_7, -4.9_8_5_9, -3.2_4_0_6, 0.8_2_6_2, -3.0_0_3_3, 1.2_9_6_4, -3.3_6_9_9]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : List[Any] = torch.tensor([-1_2.8_2_8_1, -7.4_4_5_3, -0.4_6_3_9, -8.0_6_2_5, -7.2_5_0_0, -8.0_0_0_0, -6.4_8_8_3, -7.7_6_9_5, -7.8_4_3_8, -7.0_3_1_2, -6.2_1_8_8, -7.1_3_2_8, -1.8_4_9_6, 1.9_9_6_1, -8.6_2_5_0, -6.7_2_2_7, -1_2.8_2_8_1, -6.9_4_9_2, -7.0_7_4_2, -7.7_8_5_2, -7.5_8_2_0, -7.9_0_6_2, -6.9_3_7_5, -7.9_8_0_5, -8.3_4_3_8, -8.1_5_6_2, -8.0_4_6_9, -7.6_2_5_0, -7.7_4_2_2, -7.3_3_9_8,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : Optional[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' , device_map='auto' ) lowerCAmelCase__ : List[str] = model(torch.tensor(a ) ) # Expected mean on dim = -1 lowerCAmelCase__ : Union[str, Any] = torch.tensor([[-2.0_6_2_2, -1.2_7_9_4, -1.1_6_3_8, -0.9_7_8_8, -1.4_6_0_3, -1.0_2_3_8, -1.7_8_9_3, -1.4_4_1_1]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : Any = torch.tensor([-8.1_4_0_6, -8.0_5_4_7, 2.7_4_6_1, -1.2_3_4_4, -0.1_4_4_8, -1.8_2_6_2, -1.0_0_2_0, -1.8_1_5_4, -1.6_8_9_5, -1.8_5_1_6, -2.3_5_7_4, -0.9_2_7_7, 3.7_5_9_8, 6.5_7_4_2, -1.2_9_9_8, -0.1_1_7_7, -8.1_4_0_6, -2.9_6_8_8, -2.9_1_9_9, -3.1_6_9_9, -3.5_2_5_4, -2.3_5_5_5, -2.7_9_8_8, -3.4_1_4_1, -2.8_2_6_2, -4.5_1_9_5, -3.3_3_7_9, -3.3_1_6_4, -2.7_8_3_2, -3.0_2_7_3] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : int = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : Optional[int] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' , device_map='auto' ) lowerCAmelCase__ : str = model(torch.tensor(a ) ) # Expected mean on dim = -1 lowerCAmelCase__ : str = torch.tensor([[-0.8_5_6_2, -1.8_5_2_0, -0.7_5_5_1, -0.4_1_6_2, -1.5_1_6_1, -1.2_0_3_8, -2.4_8_2_3, -2.3_2_5_4]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : List[str] = torch.tensor([-2.2_2_2_7, 4.8_8_2_8, 0.9_0_2_3, -0.4_5_7_8, -0.7_8_7_1, -0.1_0_3_3, -0.6_2_2_1, -0.5_7_8_6, -0.7_8_0_3, -1.0_6_7_4, -1.2_9_2_0, -0.1_5_7_0, 0.8_0_0_8, 2.0_7_2_3, -0.9_4_9_7, 0.2_7_7_1, -2.2_2_2_7, -0.7_6_1_2, -1.4_3_4_6, -1.2_0_6_1, -1.6_4_2_6, -0.3_0_0_0, -0.7_1_3_9, -1.1_9_3_4, -1.8_6_9_1, -1.6_9_7_3, -1.5_9_4_7, -1.2_7_0_5, -0.3_5_2_3, -0.5_5_1_3] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' , device_map='auto' ) lowerCAmelCase__ : List[str] = model(torch.tensor(a ) ) lowerCAmelCase__ : int = torch.tensor( [[-4.2_3_2_7, -3.3_3_6_0, -4.6_6_6_5, -4.7_6_3_1, -1.8_1_8_0, -3.4_1_7_0, -1.4_2_1_1, -3.1_8_1_0]] , dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # fmt: off lowerCAmelCase__ : Optional[int] = torch.tensor([-9.4_9_2_2, -3.9_5_5_1, 1.7_9_9_8, -5.6_7_5_8, -5.1_0_5_5, -5.8_9_8_4, -4.8_3_2_0, -6.8_0_8_6, -6.5_3_9_1, -5.6_1_7_2, -5.5_8_2_0, -5.5_3_5_2, 1.7_8_8_1, 3.6_2_8_9, -6.5_1_1_7, -3.4_7_8_5, -9.5_0_0_0, -6.0_3_5_2, -6.8_1_2_5, -6.0_1_9_5, -6.6_8_3_6, -5.4_7_2_7, -6.2_8_1_2, -6.0_3_9_1, -7.3_3_9_8, -7.4_2_9_7, -7.4_8_4_4, -6.5_8_2_0, -5.8_7_8_9, -5.5_3_1_2] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' lowerCAmelCase__ : Tuple = 'Simply put, the theory of relativity states that ' lowerCAmelCase__ : Dict = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) lowerCAmelCase__ : Dict = tokenizer.encode(a , return_tensors='pt' ) lowerCAmelCase__ : str = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' , device_map='sequential' , use_safetensors=a ) # greedy generation outputs lowerCAmelCase__ : Optional[Any] = model.generate(a , max_new_tokens=64 , top_p=a , temperature=1 , do_sample=a ) lowerCAmelCase__ : Tuple = tokenizer.decode(generated_ids[0] , skip_special_tokens=a ) self.assertEqual(a , a )
307
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """microsoft/swinv2-tiny-patch4-window8-256""": ( """https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json""" ), } class A__ ( __magic_name__ ): lowercase = 'swinv2' lowercase = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : Optional[int] , a : Union[str, Any]=224 , a : Tuple=4 , a : Union[str, Any]=3 , a : List[str]=96 , a : List[Any]=[2, 2, 6, 2] , a : Dict=[3, 6, 12, 24] , a : Union[str, Any]=7 , a : str=4.0 , a : int=True , a : Optional[int]=0.0 , a : Any=0.0 , a : Any=0.1 , a : Optional[Any]="gelu" , a : Tuple=False , a : Optional[int]=0.0_2 , a : List[str]=1E-5 , a : int=32 , **a : Optional[Any] , ): '''simple docstring''' super().__init__(**_SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : Tuple = image_size lowerCAmelCase__ : Tuple = patch_size lowerCAmelCase__ : Dict = num_channels lowerCAmelCase__ : str = embed_dim lowerCAmelCase__ : Union[str, Any] = depths lowerCAmelCase__ : Dict = len(_SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : List[Any] = num_heads lowerCAmelCase__ : Dict = window_size lowerCAmelCase__ : List[Any] = mlp_ratio lowerCAmelCase__ : Dict = qkv_bias lowerCAmelCase__ : Optional[Any] = hidden_dropout_prob lowerCAmelCase__ : Any = attention_probs_dropout_prob lowerCAmelCase__ : Tuple = drop_path_rate lowerCAmelCase__ : Any = hidden_act lowerCAmelCase__ : Any = use_absolute_embeddings lowerCAmelCase__ : Optional[int] = layer_norm_eps lowerCAmelCase__ : int = initializer_range lowerCAmelCase__ : str = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCAmelCase__ : str = int(embed_dim * 2 ** (len(_SCREAMING_SNAKE_CASE ) - 1) ) lowerCAmelCase__ : List[Any] = (0, 0, 0, 0)
371
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """microsoft/unispeech-large-1500h-cv""": ( """https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json""" ), # See all UniSpeech models at https://huggingface.co/models?filter=unispeech } class A__ ( __magic_name__ ): lowercase = 'unispeech' def __init__( self : Any , a : List[Any]=32 , a : List[Any]=768 , a : Any=12 , a : List[str]=12 , a : List[Any]=3_072 , a : Any="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : List[str]=0.1 , a : Union[str, Any]=0.0 , a : str=0.0 , a : int=0.1 , a : List[str]=0.1 , a : List[Any]=0.0_2 , a : Optional[int]=1E-5 , a : Optional[int]="group" , a : Optional[Any]="gelu" , a : List[Any]=(512, 512, 512, 512, 512, 512, 512) , a : Optional[Any]=(5, 2, 2, 2, 2, 2, 2) , a : List[str]=(10, 3, 3, 3, 3, 2, 2) , a : Union[str, Any]=False , a : Union[str, Any]=128 , a : Tuple=16 , a : Dict=False , a : str=True , a : str=0.0_5 , a : Union[str, Any]=10 , a : Tuple=2 , a : int=0.0 , a : Optional[Any]=10 , a : List[str]=0 , a : str=320 , a : List[str]=2 , a : Optional[Any]=0.1 , a : Any=100 , a : Dict=256 , a : Any=256 , a : Dict=0.1 , a : List[Any]="mean" , a : Dict=False , a : str=False , a : Optional[int]=256 , a : Any=80 , a : List[Any]=0 , a : Optional[int]=1 , a : int=2 , a : List[Any]=0.5 , **a : int , ): '''simple docstring''' super().__init__(**a , pad_token_id=a , bos_token_id=a , eos_token_id=a ) lowerCAmelCase__ : List[str] = hidden_size lowerCAmelCase__ : List[str] = feat_extract_norm lowerCAmelCase__ : Optional[Any] = feat_extract_activation lowerCAmelCase__ : str = list(a ) lowerCAmelCase__ : List[str] = list(a ) lowerCAmelCase__ : Tuple = list(a ) lowerCAmelCase__ : Dict = conv_bias lowerCAmelCase__ : Optional[int] = num_conv_pos_embeddings lowerCAmelCase__ : Any = num_conv_pos_embedding_groups lowerCAmelCase__ : str = len(self.conv_dim ) lowerCAmelCase__ : Any = num_hidden_layers lowerCAmelCase__ : Dict = intermediate_size lowerCAmelCase__ : Dict = hidden_act lowerCAmelCase__ : Union[str, Any] = num_attention_heads lowerCAmelCase__ : Union[str, Any] = hidden_dropout lowerCAmelCase__ : Tuple = attention_dropout lowerCAmelCase__ : str = activation_dropout lowerCAmelCase__ : Any = feat_proj_dropout lowerCAmelCase__ : List[Any] = final_dropout lowerCAmelCase__ : Tuple = layerdrop lowerCAmelCase__ : Any = layer_norm_eps lowerCAmelCase__ : Dict = initializer_range lowerCAmelCase__ : Optional[Any] = num_ctc_classes lowerCAmelCase__ : Tuple = vocab_size lowerCAmelCase__ : Dict = do_stable_layer_norm lowerCAmelCase__ : List[Any] = use_weighted_layer_sum lowerCAmelCase__ : Any = classifier_proj_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)`, but is `len(config.conv_dim) =' f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCAmelCase__ : Union[str, Any] = apply_spec_augment lowerCAmelCase__ : Any = mask_time_prob lowerCAmelCase__ : Dict = mask_time_length lowerCAmelCase__ : Tuple = mask_time_min_masks lowerCAmelCase__ : Optional[int] = mask_feature_prob lowerCAmelCase__ : Optional[Any] = mask_feature_length lowerCAmelCase__ : int = mask_feature_min_masks # parameters for pretraining with codevector quantized representations lowerCAmelCase__ : int = num_codevectors_per_group lowerCAmelCase__ : Any = num_codevector_groups lowerCAmelCase__ : Any = contrastive_logits_temperature lowerCAmelCase__ : int = feat_quantizer_dropout lowerCAmelCase__ : List[Any] = num_negatives lowerCAmelCase__ : List[str] = codevector_dim lowerCAmelCase__ : Optional[int] = proj_codevector_dim lowerCAmelCase__ : Dict = diversity_loss_weight # ctc loss lowerCAmelCase__ : Any = ctc_loss_reduction lowerCAmelCase__ : Any = ctc_zero_infinity # pretraining loss lowerCAmelCase__ : Union[str, Any] = replace_prob @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: return [sentence[i : i + ngram_size] for i in range(len(lowerCamelCase__ ) - ngram_size + 1 )] if __name__ == "__main__": from doctest import testmod testmod()
350
import torch from torch import nn class A__ ( nn.Module ): def __init__( self : Optional[int] , a : Union[str, Any] , a : str , a : str , a : List[Any] , a : List[Any]=1 , a : Tuple=False ): '''simple docstring''' super().__init__() lowerCAmelCase__ : Dict = n_token lowerCAmelCase__ : Any = d_embed lowerCAmelCase__ : str = d_proj lowerCAmelCase__ : int = cutoffs + [n_token] lowerCAmelCase__ : Union[str, Any] = [0] + self.cutoffs lowerCAmelCase__ : str = div_val lowerCAmelCase__ : Tuple = self.cutoffs[0] lowerCAmelCase__ : Dict = len(self.cutoffs ) - 1 lowerCAmelCase__ : Any = self.shortlist_size + self.n_clusters if self.n_clusters > 0: lowerCAmelCase__ : int = nn.Parameter(torch.zeros(self.n_clusters , self.d_embed ) ) lowerCAmelCase__ : Optional[Any] = nn.Parameter(torch.zeros(self.n_clusters ) ) lowerCAmelCase__ : Optional[int] = nn.ModuleList() lowerCAmelCase__ : Tuple = nn.ParameterList() if div_val == 1: for i in range(len(self.cutoffs ) ): if d_proj != d_embed: self.out_projs.append(nn.Parameter(torch.FloatTensor(a , a ) ) ) else: self.out_projs.append(a ) self.out_layers.append(nn.Linear(a , a ) ) else: for i in range(len(self.cutoffs ) ): lowerCAmelCase__ , lowerCAmelCase__ : Any = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : Optional[Any] = d_embed // (div_val**i) self.out_projs.append(nn.Parameter(torch.FloatTensor(a , a ) ) ) self.out_layers.append(nn.Linear(a , r_idx - l_idx ) ) lowerCAmelCase__ : Tuple = keep_order def _lowerCamelCase ( self : Optional[int] , a : List[str] , a : int , a : List[str] , a : str ): '''simple docstring''' if proj is None: lowerCAmelCase__ : Tuple = nn.functional.linear(a , a , bias=a ) else: # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1: lowerCAmelCase__ : int = nn.functional.linear(a , proj.t().contiguous() ) lowerCAmelCase__ : Tuple = nn.functional.linear(a , a , bias=a ) # else: # logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t())) # if bias is not None: # logit = logit + bias return logit def _lowerCamelCase ( self : List[str] , a : List[Any] , a : Optional[int]=None , a : Tuple=False ): '''simple docstring''' if labels is not None: # Shift so that tokens < n predict n lowerCAmelCase__ : str = hidden[..., :-1, :].contiguous() lowerCAmelCase__ : Optional[Any] = labels[..., 1:].contiguous() lowerCAmelCase__ : List[Any] = hidden.view(-1 , hidden.size(-1 ) ) lowerCAmelCase__ : Tuple = labels.view(-1 ) if hidden.size(0 ) != labels.size(0 ): raise RuntimeError('Input and labels should have the same size in the batch dimension.' ) else: lowerCAmelCase__ : Optional[Any] = hidden.view(-1 , hidden.size(-1 ) ) if self.n_clusters == 0: lowerCAmelCase__ : Optional[Any] = self._compute_logit(a , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) if labels is not None: lowerCAmelCase__ : str = labels != -100 lowerCAmelCase__ : int = torch.zeros_like(a , dtype=hidden.dtype , device=hidden.device ) lowerCAmelCase__ : List[str] = ( -nn.functional.log_softmax(a , dim=-1 )[mask].gather(1 , labels[mask].unsqueeze(1 ) ).squeeze(1 ) ) else: lowerCAmelCase__ : Optional[Any] = nn.functional.log_softmax(a , dim=-1 ) else: # construct weights and biases lowerCAmelCase__ , lowerCAmelCase__ : int = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : Any = self.out_layers[0].weight[l_idx:r_idx] lowerCAmelCase__ : Any = self.out_layers[0].bias[l_idx:r_idx] else: lowerCAmelCase__ : Optional[Any] = self.out_layers[i].weight lowerCAmelCase__ : Optional[int] = self.out_layers[i].bias if i == 0: lowerCAmelCase__ : Dict = torch.cat([weight_i, self.cluster_weight] , dim=0 ) lowerCAmelCase__ : Union[str, Any] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(a ) biases.append(a ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = weights[0], biases[0], self.out_projs[0] lowerCAmelCase__ : List[Any] = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : Union[str, Any] = nn.functional.log_softmax(a , dim=1 ) if labels is None: lowerCAmelCase__ : Tuple = hidden.new_empty((head_logit.size(0 ), self.n_token) ) else: lowerCAmelCase__ : Dict = torch.zeros_like(a , dtype=hidden.dtype , device=hidden.device ) lowerCAmelCase__ : Tuple = 0 lowerCAmelCase__ : Union[str, Any] = [0] + self.cutoffs for i in range(len(a ) - 1 ): lowerCAmelCase__ , lowerCAmelCase__ : Tuple = cutoff_values[i], cutoff_values[i + 1] if labels is not None: lowerCAmelCase__ : Tuple = (labels >= l_idx) & (labels < r_idx) lowerCAmelCase__ : int = mask_i.nonzero().squeeze() if indices_i.numel() == 0: continue lowerCAmelCase__ : Tuple = labels.index_select(0 , a ) - l_idx lowerCAmelCase__ : Any = head_logprob.index_select(0 , a ) lowerCAmelCase__ : Optional[int] = hidden.index_select(0 , a ) else: lowerCAmelCase__ : Any = hidden if i == 0: if labels is not None: lowerCAmelCase__ : Union[str, Any] = head_logprob_i.gather(1 , target_i[:, None] ).squeeze(1 ) else: lowerCAmelCase__ : List[str] = head_logprob[:, : self.cutoffs[0]] else: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Any = weights[i], biases[i], self.out_projs[i] lowerCAmelCase__ : Union[str, Any] = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : Optional[int] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : List[Any] = self.cutoffs[0] + i - 1 # No probability for the head cluster if labels is not None: lowerCAmelCase__ : List[str] = head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather( 1 , target_i[:, None] ).squeeze(1 ) else: lowerCAmelCase__ : Tuple = head_logprob[:, cluster_prob_idx, None] + tail_logprob_i lowerCAmelCase__ : Union[str, Any] = logprob_i if labels is not None: if (hasattr(self , 'keep_order' ) and self.keep_order) or keep_order: out.index_copy_(0 , a , -logprob_i ) else: out[offset : offset + logprob_i.size(0 )].copy_(-logprob_i ) offset += logprob_i.size(0 ) return out def _lowerCamelCase ( self : List[Any] , a : Any ): '''simple docstring''' if self.n_clusters == 0: lowerCAmelCase__ : Union[str, Any] = self._compute_logit(a , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) return nn.functional.log_softmax(a , dim=-1 ) else: # construct weights and biases lowerCAmelCase__ , lowerCAmelCase__ : str = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : str = self.out_layers[0].weight[l_idx:r_idx] lowerCAmelCase__ : Dict = self.out_layers[0].bias[l_idx:r_idx] else: lowerCAmelCase__ : int = self.out_layers[i].weight lowerCAmelCase__ : int = self.out_layers[i].bias if i == 0: lowerCAmelCase__ : Optional[int] = torch.cat([weight_i, self.cluster_weight] , dim=0 ) lowerCAmelCase__ : Union[str, Any] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(a ) biases.append(a ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : str = weights[0], biases[0], self.out_projs[0] lowerCAmelCase__ : Dict = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : List[Any] = hidden.new_empty((head_logit.size(0 ), self.n_token) ) lowerCAmelCase__ : Optional[Any] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : List[Any] = [0] + self.cutoffs for i in range(len(a ) - 1 ): lowerCAmelCase__ , lowerCAmelCase__ : str = cutoff_values[i], cutoff_values[i + 1] if i == 0: lowerCAmelCase__ : Union[str, Any] = head_logprob[:, : self.cutoffs[0]] else: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = weights[i], biases[i], self.out_projs[i] lowerCAmelCase__ : Dict = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : List[str] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : Dict = head_logprob[:, -i] + tail_logprob_i lowerCAmelCase__ : List[str] = logprob_i return out
307
0
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class A__ ( __SCREAMING_SNAKE_CASE ): lowercase = (KDPMaDiscreteScheduler,) lowercase = 10 def _lowerCamelCase ( self : str , **a : str ): '''simple docstring''' lowerCAmelCase__ : Tuple = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_snake_case ) return config def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_snake_case ) def _lowerCamelCase ( self : str ): '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_snake_case , beta_end=_snake_case ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_snake_case ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_snake_case ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.scheduler_classes[0] lowerCAmelCase__ : Union[str, Any] = self.get_scheduler_config(prediction_type='v_prediction' ) lowerCAmelCase__ : Union[str, Any] = scheduler_class(**_snake_case ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Optional[Any] = self.dummy_model() lowerCAmelCase__ : Dict = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : Union[str, Any] = sample.to(_snake_case ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : str = scheduler.scale_model_input(_snake_case , _snake_case ) lowerCAmelCase__ : Optional[int] = model(_snake_case , _snake_case ) lowerCAmelCase__ : int = scheduler.step(_snake_case , _snake_case , _snake_case ) lowerCAmelCase__ : Optional[int] = output.prev_sample lowerCAmelCase__ : Optional[int] = torch.sum(torch.abs(_snake_case ) ) lowerCAmelCase__ : Optional[int] = torch.mean(torch.abs(_snake_case ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6934E-07 ) < 1E-2 assert abs(result_mean.item() - 6.1112E-10 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 4.693428650170972E-07 ) < 1E-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1E-3 def _lowerCamelCase ( self : Any ): '''simple docstring''' if torch_device == "mps": return lowerCAmelCase__ : Optional[int] = self.scheduler_classes[0] lowerCAmelCase__ : List[Any] = self.get_scheduler_config() lowerCAmelCase__ : Dict = scheduler_class(**_snake_case ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Optional[int] = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : Optional[int] = sample.to(_snake_case ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : Tuple = scheduler.scale_model_input(_snake_case , _snake_case ) lowerCAmelCase__ : Optional[Any] = model(_snake_case , _snake_case ) lowerCAmelCase__ : Dict = scheduler.step(_snake_case , _snake_case , _snake_case ) lowerCAmelCase__ : List[Any] = output.prev_sample lowerCAmelCase__ : Any = torch.sum(torch.abs(_snake_case ) ) lowerCAmelCase__ : Optional[int] = torch.mean(torch.abs(_snake_case ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1E-3 def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' if torch_device == "mps": return lowerCAmelCase__ : List[str] = self.scheduler_classes[0] lowerCAmelCase__ : List[str] = self.get_scheduler_config() lowerCAmelCase__ : int = scheduler_class(**_snake_case ) scheduler.set_timesteps(self.num_inference_steps , device=_snake_case ) lowerCAmelCase__ : Optional[int] = self.dummy_model() lowerCAmelCase__ : Tuple = self.dummy_sample_deter.to(_snake_case ) * scheduler.init_noise_sigma for t in scheduler.timesteps: lowerCAmelCase__ : int = scheduler.scale_model_input(_snake_case , _snake_case ) lowerCAmelCase__ : Optional[int] = model(_snake_case , _snake_case ) lowerCAmelCase__ : List[Any] = scheduler.step(_snake_case , _snake_case , _snake_case ) lowerCAmelCase__ : Optional[Any] = output.prev_sample lowerCAmelCase__ : Tuple = torch.sum(torch.abs(_snake_case ) ) lowerCAmelCase__ : Union[str, Any] = torch.mean(torch.abs(_snake_case ) ) if str(_snake_case ).startswith('cpu' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1E-3
351
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate lowerCamelCase__ = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow("""""", """|""", """|"""), datarow=DataRow("""""", """|""", """|"""), padding=1, with_header_hide=None, ) lowerCamelCase__ = [] lowerCamelCase__ = [] lowerCamelCase__ = {"""type""": """section""", """text""": {"""type""": """plain_text""", """text""": """No failed tests! 🤗""", """emoji""": True}} lowerCamelCase__ = [ { """type""": """header""", """text""": { """type""": """plain_text""", """text""": F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", """emoji""": True, }, } ] lowerCamelCase__ = 0 for log in Path().glob("""*.log"""): lowerCamelCase__ = 0 with open(log, """r""") as f: for line in f: lowerCamelCase__ = json.loads(line) if line.get("""nodeid""", """""") != "": lowerCamelCase__ = line["""nodeid"""] if line.get("""duration""", None) is not None: lowerCamelCase__ = F"""{line["duration"]:.4f}""" if line.get("""outcome""", """""") == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split("""_""")[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) lowerCamelCase__ = [] log.unlink() lowerCamelCase__ = """""" lowerCamelCase__ = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" lowerCamelCase__ = [] lowerCamelCase__ = {} for test in failed_tests: lowerCamelCase__ = test[0].split("""::""") lowerCamelCase__ = data[0].split("""/""")[-1] if data[0] not in filesafailed: lowerCamelCase__ = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) lowerCamelCase__ = [test[0] for test in failed_table] lowerCamelCase__ = list(set(files)) # Count number of instances in failed_tests lowerCamelCase__ = [] for file in individual_files: table.append([file, len(filesafailed[file])]) lowerCamelCase__ = tabulate( table, headers=["""Test Location""", """Num Failed"""], tablefmt=hf_table_format, stralign="""right""", ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 3000: lowerCamelCase__ = """Too many failed tests, please see the full report in the Action results.""" lowerCamelCase__ = len(err) + 10 lowerCamelCase__ = message[: 3000 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: lowerCamelCase__ = """No failed tests! 🤗""" print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get("""TEST_TYPE""", """""") != "": from slack_sdk import WebClient lowerCamelCase__ = WebClient(token=os.environ["""SLACK_API_TOKEN"""]) if message != "No failed tests! 🤗": lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": message, }, } payload.append(md_report) lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": """*For more details:*""", }, """accessory""": { """type""": """button""", """text""": { """type""": """plain_text""", """text""": """Check Action results""", """emoji""": True, }, """url""": F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) lowerCamelCase__ = { """type""": """context""", """elements""": [ { """type""": """plain_text""", """text""": F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) lowerCamelCase__ = client.chat_postMessage(channel="""#accelerate-ci-daily""", text=message, blocks=payload) lowerCamelCase__ = response.data["""ts"""] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name lowerCamelCase__ = """""" for i, row in enumerate(test_failures): if row[0] != test_class: lowerCamelCase__ = row[0] else: lowerCamelCase__ = """""" lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel="""#accelerate-ci-daily""", thread_ts=ts, blocks=[payload], )
307
0
import numpy as np from transformers import Pipeline def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> int: lowerCAmelCase__ : List[str] = np.max(UpperCamelCase__ , axis=-1 , keepdims=UpperCamelCase__ ) lowerCAmelCase__ : int = np.exp(outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=UpperCamelCase__ ) class A__ ( __magic_name__ ): def _lowerCamelCase ( self : int , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = {} if "second_text" in kwargs: lowerCAmelCase__ : Any = kwargs['''second_text'''] return preprocess_kwargs, {}, {} def _lowerCamelCase ( self : Tuple , a : Optional[Any] , a : Union[str, Any]=None ): '''simple docstring''' return self.tokenizer(a , text_pair=a , return_tensors=self.framework ) def _lowerCamelCase ( self : List[Any] , a : List[str] ): '''simple docstring''' return self.model(**a ) def _lowerCamelCase ( self : List[Any] , a : int ): '''simple docstring''' lowerCAmelCase__ : int = model_outputs.logits[0].numpy() lowerCAmelCase__ : Optional[Any] = softmax(a ) lowerCAmelCase__ : str = np.argmax(a ) lowerCAmelCase__ : int = self.model.config.idalabel[best_class] lowerCAmelCase__ : List[str] = probabilities[best_class].item() lowerCAmelCase__ : Dict = logits.tolist() return {"label": label, "score": score, "logits": logits}
352
import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs lowerCamelCase__ = imread(r"""digital_image_processing/image_data/lena_small.jpg""") lowerCamelCase__ = cvtColor(img, COLOR_BGR2GRAY) def lowerCAmelCase__ ( ) -> Dict: lowerCAmelCase__ : List[Any] = cn.convert_to_negative(SCREAMING_SNAKE_CASE_ ) # assert negative_img array for at least one True assert negative_img.any() def lowerCAmelCase__ ( ) -> Optional[Any]: with Image.open('digital_image_processing/image_data/lena_small.jpg' ) as img: # Work around assertion for response assert str(cc.change_contrast(SCREAMING_SNAKE_CASE_ , 110 ) ).startswith( '<PIL.Image.Image image mode=RGB size=100x100 at' ) def lowerCAmelCase__ ( ) -> Tuple: lowerCAmelCase__ : str = canny.gen_gaussian_kernel(9 , sigma=1.4 ) # Assert ambiguous array assert resp.all() def lowerCAmelCase__ ( ) -> Tuple: lowerCAmelCase__ : Tuple = imread('digital_image_processing/image_data/lena_small.jpg' , 0 ) # assert ambiguous array for all == True assert canny_img.all() lowerCAmelCase__ : Optional[Any] = canny.canny(SCREAMING_SNAKE_CASE_ ) # assert canny array for at least one True assert canny_array.any() def lowerCAmelCase__ ( ) -> Optional[int]: assert gg.gaussian_filter(SCREAMING_SNAKE_CASE_ , 5 , sigma=0.9 ).all() def lowerCAmelCase__ ( ) -> Dict: # laplace diagonals lowerCAmelCase__ : Union[str, Any] = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]] ) lowerCAmelCase__ : int = conv.img_convolve(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).astype(SCREAMING_SNAKE_CASE_ ) assert res.any() def lowerCAmelCase__ ( ) -> List[str]: assert med.median_filter(SCREAMING_SNAKE_CASE_ , 3 ).any() def lowerCAmelCase__ ( ) -> Any: lowerCAmelCase__ , lowerCAmelCase__ : str = sob.sobel_filter(SCREAMING_SNAKE_CASE_ ) assert grad.any() and theta.any() def lowerCAmelCase__ ( ) -> Any: lowerCAmelCase__ : int = sp.make_sepia(SCREAMING_SNAKE_CASE_ , 20 ) assert sepia.all() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = "digital_image_processing/image_data/lena_small.jpg" ) -> Optional[Any]: lowerCAmelCase__ : List[Any] = bs.Burkes(imread(SCREAMING_SNAKE_CASE_ , 1 ) , 120 ) burkes.process() assert burkes.output_img.any() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = "digital_image_processing/image_data/lena_small.jpg" , ) -> Any: lowerCAmelCase__ : Dict = rs.NearestNeighbour(imread(SCREAMING_SNAKE_CASE_ , 1 ) , 400 , 200 ) nn.process() assert nn.output.any() def lowerCAmelCase__ ( ) -> int: lowerCAmelCase__ : int = 'digital_image_processing/image_data/lena.jpg' # Reading the image and converting it to grayscale. lowerCAmelCase__ : List[str] = imread(SCREAMING_SNAKE_CASE_ , 0 ) # Test for get_neighbors_pixel function() return not None lowerCAmelCase__ : str = 0 lowerCAmelCase__ : str = 0 lowerCAmelCase__ : List[str] = image[x_coordinate][y_coordinate] lowerCAmelCase__ : Dict = lbp.get_neighbors_pixel( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image lowerCAmelCase__ : List[str] = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 , image.shape[0] ): for j in range(0 , image.shape[1] ): lowerCAmelCase__ : Dict = lbp.local_binary_value(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert lbp_image.any()
307
0
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mvp import MvpTokenizer lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} # See all MVP models at https://huggingface.co/models?filter=mvp lowerCamelCase__ = { 'vocab_file': { 'RUCAIBox/mvp': 'https://huggingface.co/RUCAIBox/mvp/resolve/main/vocab.json', }, 'added_tokens.json': { 'RUCAIBox/mvp': 'https://huggingface.co/RUCAIBox/mvp/resolve/main/added_tokens.json', }, 'merges_file': { 'RUCAIBox/mvp': 'https://huggingface.co/RUCAIBox/mvp/resolve/main/merges.txt', }, 'tokenizer_file': { 'RUCAIBox/mvp': 'https://huggingface.co/RUCAIBox/mvp/resolve/main/tokenizer.json', }, } lowerCamelCase__ = { 'RUCAIBox/mvp': 1024, } class A__ ( _lowerCamelCase ): lowercase = VOCAB_FILES_NAMES lowercase = PRETRAINED_VOCAB_FILES_MAP lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase = ['input_ids', 'attention_mask'] lowercase = MvpTokenizer def __init__( self : Dict , a : List[str]=None , a : Optional[int]=None , a : Union[str, Any]=None , a : Tuple="replace" , a : Dict="<s>" , a : List[str]="</s>" , a : Union[str, Any]="</s>" , a : Optional[Any]="<s>" , a : Optional[int]="<unk>" , a : Optional[Any]="<pad>" , a : int="<mask>" , a : int=False , a : int=True , **a : Any , ): '''simple docstring''' super().__init__( a , a , tokenizer_file=a , errors=a , bos_token=a , eos_token=a , sep_token=a , cls_token=a , unk_token=a , pad_token=a , mask_token=a , add_prefix_space=a , trim_offsets=a , **a , ) lowerCAmelCase__ : Tuple = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , a ) != add_prefix_space: lowerCAmelCase__ : Union[str, Any] = getattr(a , pre_tok_state.pop('type' ) ) lowerCAmelCase__ : Any = add_prefix_space lowerCAmelCase__ : Optional[int] = pre_tok_class(**a ) lowerCAmelCase__ : int = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` lowerCAmelCase__ : Optional[Any] = 'post_processor' lowerCAmelCase__ : Optional[int] = getattr(self.backend_tokenizer , a , a ) if tokenizer_component_instance: lowerCAmelCase__ : int = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowerCAmelCase__ : List[str] = tuple(state['sep'] ) if "cls" in state: lowerCAmelCase__ : str = tuple(state['cls'] ) lowerCAmelCase__ : int = False if state.get('add_prefix_space' , a ) != add_prefix_space: lowerCAmelCase__ : Optional[int] = add_prefix_space lowerCAmelCase__ : List[Any] = True if state.get('trim_offsets' , a ) != trim_offsets: lowerCAmelCase__ : int = trim_offsets lowerCAmelCase__ : Optional[Any] = True if changes_to_apply: lowerCAmelCase__ : Optional[Any] = getattr(a , state.pop('type' ) ) lowerCAmelCase__ : Tuple = component_class(**a ) setattr(self.backend_tokenizer , a , a ) @property def _lowerCamelCase ( self : Tuple ): '''simple docstring''' 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 _lowerCamelCase ( self : Optional[Any] , a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : str = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else value lowerCAmelCase__ : Dict = value def _lowerCamelCase ( self : int , *a : Union[str, Any] , **a : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = kwargs.get('is_split_into_words' , a ) 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(*a , **a ) def _lowerCamelCase ( self : int , *a : Tuple , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : List[Any] = kwargs.get('is_split_into_words' , a ) 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(*a , **a ) def _lowerCamelCase ( self : List[str] , a : str , a : Optional[str] = None ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self._tokenizer.model.save(a , name=a ) return tuple(a ) def _lowerCamelCase ( self : List[str] , a : List[Any] , a : Optional[int]=None ): '''simple docstring''' lowerCAmelCase__ : Dict = [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 _lowerCamelCase ( self : List[Any] , a : List[int] , a : Optional[List[int]] = None ): '''simple docstring''' lowerCAmelCase__ : Dict = [self.sep_token_id] lowerCAmelCase__ : int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
353
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCamelCase__ = { """configuration_blip""": [ """BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BlipConfig""", """BlipTextConfig""", """BlipVisionConfig""", ], """processing_blip""": ["""BlipProcessor"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""BlipImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """BlipModel""", """BlipPreTrainedModel""", """BlipForConditionalGeneration""", """BlipForQuestionAnswering""", """BlipVisionModel""", """BlipTextModel""", """BlipForImageTextRetrieval""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFBlipModel""", """TFBlipPreTrainedModel""", """TFBlipForConditionalGeneration""", """TFBlipForQuestionAnswering""", """TFBlipVisionModel""", """TFBlipTextModel""", """TFBlipForImageTextRetrieval""", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
from __future__ import annotations def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int | float: if len(_lowerCAmelCase ) == 0: raise ValueError('find_max() arg is an empty sequence' ) if ( left >= len(_lowerCAmelCase ) or left < -len(_lowerCAmelCase ) or right >= len(_lowerCAmelCase ) or right < -len(_lowerCAmelCase ) ): raise IndexError('list index out of range' ) if left == right: return nums[left] lowerCAmelCase__ : Any = (left + right) >> 1 # the middle lowerCAmelCase__ : Any = find_max(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # find max in range[left, mid] lowerCAmelCase__ : str = find_max(_lowerCAmelCase , mid + 1 , _lowerCAmelCase ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
354
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=0.999 , SCREAMING_SNAKE_CASE_="cosine" , ) -> Union[str, Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(SCREAMING_SNAKE_CASE_ ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(SCREAMING_SNAKE_CASE_ ): return math.exp(t * -12.0 ) else: raise ValueError(F'''Unsupported alpha_tranform_type: {alpha_transform_type}''' ) lowerCAmelCase__ : Tuple = [] for i in range(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : List[Any] = i / num_diffusion_timesteps lowerCAmelCase__ : str = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) return torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.floataa ) class A__ ( __magic_name__ , __magic_name__ ): lowercase = [e.name for e in KarrasDiffusionSchedulers] lowercase = 2 @register_to_config def __init__( self : Union[str, Any] , a : int = 1_000 , a : float = 0.0_0_0_8_5 , a : float = 0.0_1_2 , a : str = "linear" , a : Optional[Union[np.ndarray, List[float]]] = None , a : str = "epsilon" , a : Optional[bool] = False , a : Optional[bool] = False , a : float = 1.0 , a : str = "linspace" , a : int = 0 , ): '''simple docstring''' if trained_betas is not None: lowerCAmelCase__ : List[str] = torch.tensor(a , dtype=torch.floataa ) elif beta_schedule == "linear": lowerCAmelCase__ : List[str] = torch.linspace(a , a , a , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowerCAmelCase__ : Union[str, Any] = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , a , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowerCAmelCase__ : int = betas_for_alpha_bar(a , alpha_transform_type='cosine' ) elif beta_schedule == "exp": lowerCAmelCase__ : List[str] = betas_for_alpha_bar(a , alpha_transform_type='exp' ) else: raise NotImplementedError(f'''{beta_schedule} does is not implemented for {self.__class__}''' ) lowerCAmelCase__ : int = 1.0 - self.betas lowerCAmelCase__ : Tuple = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(a , a , a ) lowerCAmelCase__ : Optional[Any] = use_karras_sigmas def _lowerCamelCase ( self : str , a : List[Any] , a : str=None ): '''simple docstring''' if schedule_timesteps is None: lowerCAmelCase__ : List[str] = self.timesteps lowerCAmelCase__ : int = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: lowerCAmelCase__ : List[str] = 1 if len(a ) > 1 else 0 else: lowerCAmelCase__ : Tuple = timestep.cpu().item() if torch.is_tensor(a ) else timestep lowerCAmelCase__ : Tuple = self._index_counter[timestep_int] return indices[pos].item() @property def _lowerCamelCase ( self : Dict ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _lowerCamelCase ( self : Tuple , a : torch.FloatTensor , a : Union[float, torch.FloatTensor] , ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.index_for_timestep(a ) lowerCAmelCase__ : Any = self.sigmas[step_index] lowerCAmelCase__ : Optional[Any] = sample / ((sigma**2 + 1) ** 0.5) return sample def _lowerCamelCase ( self : List[str] , a : int , a : Union[str, torch.device] = None , a : Optional[int] = None , ): '''simple docstring''' lowerCAmelCase__ : Any = num_inference_steps lowerCAmelCase__ : Union[str, Any] = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": lowerCAmelCase__ : Union[str, Any] = np.linspace(0 , num_train_timesteps - 1 , a , dtype=a )[::-1].copy() elif self.config.timestep_spacing == "leading": lowerCAmelCase__ : List[Any] = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : Dict = (np.arange(0 , a ) * step_ratio).round()[::-1].copy().astype(a ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": lowerCAmelCase__ : Tuple = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : int = (np.arange(a , 0 , -step_ratio )).round().copy().astype(a ) timesteps -= 1 else: raise ValueError( f'''{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.''' ) lowerCAmelCase__ : str = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) lowerCAmelCase__ : List[Any] = np.log(a ) lowerCAmelCase__ : Optional[int] = np.interp(a , np.arange(0 , len(a ) ) , a ) if self.config.use_karras_sigmas: lowerCAmelCase__ : str = self._convert_to_karras(in_sigmas=a , num_inference_steps=self.num_inference_steps ) lowerCAmelCase__ : Union[str, Any] = np.array([self._sigma_to_t(a , a ) for sigma in sigmas] ) lowerCAmelCase__ : Tuple = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) lowerCAmelCase__ : Dict = torch.from_numpy(a ).to(device=a ) lowerCAmelCase__ : Union[str, Any] = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) lowerCAmelCase__ : Tuple = torch.from_numpy(a ) lowerCAmelCase__ : List[str] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(a ).startswith('mps' ): # mps does not support float64 lowerCAmelCase__ : Optional[Any] = timesteps.to(a , dtype=torch.floataa ) else: lowerCAmelCase__ : Any = timesteps.to(device=a ) # empty dt and derivative lowerCAmelCase__ : str = None lowerCAmelCase__ : Optional[int] = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter lowerCAmelCase__ : Optional[Any] = defaultdict(a ) def _lowerCamelCase ( self : Any , a : Dict , a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.log(a ) # get distribution lowerCAmelCase__ : Tuple = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range lowerCAmelCase__ : Optional[int] = np.cumsum((dists >= 0) , axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) lowerCAmelCase__ : List[str] = low_idx + 1 lowerCAmelCase__ : List[str] = log_sigmas[low_idx] lowerCAmelCase__ : Any = log_sigmas[high_idx] # interpolate sigmas lowerCAmelCase__ : Union[str, Any] = (low - log_sigma) / (low - high) lowerCAmelCase__ : List[Any] = np.clip(a , 0 , 1 ) # transform interpolation to time range lowerCAmelCase__ : List[Any] = (1 - w) * low_idx + w * high_idx lowerCAmelCase__ : Any = t.reshape(sigma.shape ) return t def _lowerCamelCase ( self : Tuple , a : torch.FloatTensor , a : Any ): '''simple docstring''' lowerCAmelCase__ : float = in_sigmas[-1].item() lowerCAmelCase__ : float = in_sigmas[0].item() lowerCAmelCase__ : Tuple = 7.0 # 7.0 is the value used in the paper lowerCAmelCase__ : Tuple = np.linspace(0 , 1 , a ) lowerCAmelCase__ : Any = sigma_min ** (1 / rho) lowerCAmelCase__ : Optional[Any] = sigma_max ** (1 / rho) lowerCAmelCase__ : Tuple = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def _lowerCamelCase ( self : Any ): '''simple docstring''' return self.dt is None def _lowerCamelCase ( self : List[str] , a : Union[torch.FloatTensor, np.ndarray] , a : Union[float, torch.FloatTensor] , a : Union[torch.FloatTensor, np.ndarray] , a : bool = True , ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.index_for_timestep(a ) # advance index counter by 1 lowerCAmelCase__ : Tuple = timestep.cpu().item() if torch.is_tensor(a ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: lowerCAmelCase__ : Union[str, Any] = self.sigmas[step_index] lowerCAmelCase__ : Union[str, Any] = self.sigmas[step_index + 1] else: # 2nd order / Heun's method lowerCAmelCase__ : int = self.sigmas[step_index - 1] lowerCAmelCase__ : Any = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API lowerCAmelCase__ : Optional[int] = 0 lowerCAmelCase__ : Union[str, Any] = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": lowerCAmelCase__ : int = sigma_hat if self.state_in_first_order else sigma_next lowerCAmelCase__ : Any = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase__ : Dict = sigma_hat if self.state_in_first_order else sigma_next lowerCAmelCase__ : List[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": lowerCAmelCase__ : int = model_output else: raise ValueError( f'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`''' ) if self.config.clip_sample: lowerCAmelCase__ : str = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order lowerCAmelCase__ : Dict = (sample - pred_original_sample) / sigma_hat # 3. delta timestep lowerCAmelCase__ : Optional[int] = sigma_next - sigma_hat # store for 2nd order step lowerCAmelCase__ : List[Any] = derivative lowerCAmelCase__ : str = dt lowerCAmelCase__ : Dict = sample else: # 2. 2nd order / Heun's method lowerCAmelCase__ : Union[str, Any] = (sample - pred_original_sample) / sigma_next lowerCAmelCase__ : Union[str, Any] = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample lowerCAmelCase__ : Dict = self.dt lowerCAmelCase__ : Optional[int] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" lowerCAmelCase__ : List[str] = None lowerCAmelCase__ : Tuple = None lowerCAmelCase__ : str = None lowerCAmelCase__ : Tuple = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=a ) def _lowerCamelCase ( self : int , a : torch.FloatTensor , a : torch.FloatTensor , a : torch.FloatTensor , ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(a ): # mps does not support float64 lowerCAmelCase__ : Optional[int] = self.timesteps.to(original_samples.device , dtype=torch.floataa ) lowerCAmelCase__ : int = timesteps.to(original_samples.device , dtype=torch.floataa ) else: lowerCAmelCase__ : Union[str, Any] = self.timesteps.to(original_samples.device ) lowerCAmelCase__ : Optional[Any] = timesteps.to(original_samples.device ) lowerCAmelCase__ : List[Any] = [self.index_for_timestep(a , a ) for t in timesteps] lowerCAmelCase__ : List[str] = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): lowerCAmelCase__ : Any = sigma.unsqueeze(-1 ) lowerCAmelCase__ : List[str] = original_samples + noise * sigma return noisy_samples def __len__( self : int ): '''simple docstring''' return self.config.num_train_timesteps
307
0
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase__ = [ "word_embeddings_layernorm.weight", "word_embeddings_layernorm.bias", "input_layernorm.weight", "input_layernorm.bias", "post_attention_layernorm.weight", "post_attention_layernorm.bias", "self_attention.dense.bias", "mlp.dense_4h_to_h.bias", "ln_f.weight", "ln_f.bias", ] lowerCamelCase__ = [ "mlp.dense_4h_to_h.weight", "self_attention.dense.weight", ] def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: lowerCAmelCase__ : int = { 'word_embeddings.weight': 'word_embeddings.weight', 'word_embeddings.norm.weight': 'word_embeddings_layernorm.weight', 'word_embeddings.norm.bias': 'word_embeddings_layernorm.bias', 'weight': 'ln_f.weight', 'bias': 'ln_f.bias', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks lowerCAmelCase__ : Any = int(re.match(r'.*layer_(\d*).*' , SCREAMING_SNAKE_CASE_ )[1] ) layer_number -= 3 return F'''h.{layer_number}.''' + key def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: if dtype == torch.bool: return 1 / 8 lowerCAmelCase__ : Dict = re.search(r'[^\d](\d+)$' , str(SCREAMING_SNAKE_CASE_ ) ) if bit_search is None: raise ValueError(F'''`dtype` is not a valid dtype: {dtype}.''' ) lowerCAmelCase__ : Tuple = int(bit_search.groups()[0] ) return bit_size // 8 def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: if bloom_config_file == "": lowerCAmelCase__ : List[str] = BloomConfig() else: lowerCAmelCase__ : str = BloomConfig.from_json_file(SCREAMING_SNAKE_CASE_ ) if shard_model: lowerCAmelCase__ : int = os.listdir(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : List[str] = sorted(filter(lambda SCREAMING_SNAKE_CASE_ : s.startswith('layer' ) and "model_00" in s , SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase__ : str = {'weight_map': {}, 'metadata': {}} lowerCAmelCase__ : Union[str, Any] = 0 lowerCAmelCase__ : Union[str, Any] = None lowerCAmelCase__ : Any = BloomConfig() for j, file in enumerate(SCREAMING_SNAKE_CASE_ ): print('Processing file: {}'.format(SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase__ : str = None for i in range(SCREAMING_SNAKE_CASE_ ): # load all TP files lowerCAmelCase__ : str = file.replace('model_00' , F'''model_0{i}''' ) lowerCAmelCase__ : Optional[int] = torch.load(os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , map_location='cpu' ) # Rename keys in the transformers names lowerCAmelCase__ : Optional[Any] = list(temp.keys() ) for key in keys: lowerCAmelCase__ : int = temp.pop(SCREAMING_SNAKE_CASE_ ) if tensors is None: lowerCAmelCase__ : Optional[Any] = temp else: for key in tensors.keys(): if any(key.endswith(SCREAMING_SNAKE_CASE_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel lowerCAmelCase__ : str = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks lowerCAmelCase__ : Tuple = torch.cat([tensors[key], temp[key]] , dim=SCREAMING_SNAKE_CASE_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(SCREAMING_SNAKE_CASE_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): lowerCAmelCase__ : Any = tensors[key] / pretraining_tp torch.save( SCREAMING_SNAKE_CASE_ , os.path.join( SCREAMING_SNAKE_CASE_ , 'pytorch_model_{}-of-{}.bin'.format(str(j + 1 ).zfill(5 ) , str(len(SCREAMING_SNAKE_CASE_ ) ).zfill(5 ) ) , ) , ) for key in tensors.keys(): lowerCAmelCase__ : Tuple = tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: lowerCAmelCase__ : Dict = 'pytorch_model_{}-of-{}.bin'.format( str(j + 1 ).zfill(5 ) , str(len(SCREAMING_SNAKE_CASE_ ) ).zfill(5 ) ) lowerCAmelCase__ : int = BloomConfig() lowerCAmelCase__ : str = pytorch_dump_folder_path + '/' + CONFIG_NAME lowerCAmelCase__ : Optional[int] = total_size with open(SCREAMING_SNAKE_CASE_ , 'w' , encoding='utf-8' ) as f: f.write(config.to_json_string() ) with open(os.path.join(SCREAMING_SNAKE_CASE_ , WEIGHTS_NAME + '.index.json' ) , 'w' , encoding='utf-8' ) as f: lowerCAmelCase__ : List[Any] = json.dumps(SCREAMING_SNAKE_CASE_ , indent=2 , sort_keys=SCREAMING_SNAKE_CASE_ ) + '\n' f.write(SCREAMING_SNAKE_CASE_ ) else: lowerCAmelCase__ : Union[str, Any] = BloomModel(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : List[Any] = os.listdir(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = sorted(filter(lambda SCREAMING_SNAKE_CASE_ : s.startswith('layer' ) and "model_00" in s , SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase__ : Any = None for i, file in enumerate(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : List[Any] = None for i in range(SCREAMING_SNAKE_CASE_ ): # load all TP files lowerCAmelCase__ : Optional[int] = file.replace('model_00' , F'''model_0{i}''' ) lowerCAmelCase__ : List[Any] = torch.load(os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , map_location='cpu' ) # Rename keys in the transformers names lowerCAmelCase__ : int = list(temp.keys() ) for key in keys: lowerCAmelCase__ : List[str] = temp.pop(SCREAMING_SNAKE_CASE_ ) if tensors is None: lowerCAmelCase__ : List[str] = temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(SCREAMING_SNAKE_CASE_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel lowerCAmelCase__ : Optional[int] = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks lowerCAmelCase__ : Union[str, Any] = torch.cat([tensors[key], temp[key]] , dim=SCREAMING_SNAKE_CASE_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(SCREAMING_SNAKE_CASE_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): lowerCAmelCase__ : List[Any] = tensors[key] / pretraining_tp lowerCAmelCase__ : List[Any] = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) assert not other_keys.unexpected_keys, F'''The keys {other_keys.unexpected_keys} are unexpected''' if missing_keys is None: lowerCAmelCase__ : Dict = set(other_keys.missing_keys ) else: lowerCAmelCase__ : Union[str, Any] = missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, F'''The keys {missing_keys} are missing''' # Save pytorch-model os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = pytorch_dump_folder_path + '/' + WEIGHTS_NAME lowerCAmelCase__ : Optional[Any] = pytorch_dump_folder_path + '/' + CONFIG_NAME print(F'''Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}''' ) if config.torch_dtype is not None: lowerCAmelCase__ : int = model.to(config.torch_dtype ) torch.save(model.state_dict() , SCREAMING_SNAKE_CASE_ ) print(F'''Save configuration file to {pytorch_config_dump_path}''' ) with open(SCREAMING_SNAKE_CASE_ , 'w' , encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--bloom_checkpoint_path""", default=None, type=str, required=True, help="""Path to the Megatron-LM checkpoint path.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) parser.add_argument( """--bloom_config_file""", default="""""", type=str, help=( """An optional config json file corresponding to the pre-trained model. \n""" """This specifies the model architecture.""" ), ) parser.add_argument( """--shard_model""", action="""store_true""", help="""An optional setting to shard the output model \nThis enables sharding the converted checkpoint""", ) parser.add_argument( """--pretraining_tp""", default=4, type=int, help="""Pretraining TP rank that has been used when training the model in Megatron-LM \n""", ) lowerCamelCase__ = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
355
from __future__ import annotations import collections import tempfile import unittest import numpy as np from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import is_tf_available, is_vision_available from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_tf_bert import TFBertModelTester from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester from ..deit.test_modeling_tf_deit import TFDeiTModelTester from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester from ..vit.test_modeling_tf_vit import TFViTModelTester if is_tf_available(): from transformers import ( TFBertModel, TFCLIPVisionModel, TFDeiTModel, TFRobertaModel, TFVisionTextDualEncoderModel, TFViTModel, VisionTextDualEncoderConfig, ) if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: if isinstance(SCREAMING_SNAKE_CASE_ , collections.abc.Iterable ): return x return (x, x) @require_tf class A__ : def _lowerCamelCase ( self : List[Any] , a : List[str] , a : Optional[Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : Dict ): '''simple docstring''' pass def _lowerCamelCase ( self : Dict , a : int , a : str , a : List[Any] , a : Dict , a : List[str]=None , **a : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = VisionTextDualEncoderConfig.from_vision_text_configs(a , a ) lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel(a ) lowerCAmelCase__ : Tuple = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], config.projection_dim) ) def _lowerCamelCase ( self : Union[str, Any] , a : Dict , a : Tuple , a : Dict , a : Union[str, Any] , a : List[Any]=None , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.get_vision_text_model(a , a ) lowerCAmelCase__ : List[Any] = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : Optional[int] = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def _lowerCamelCase ( self : List[str] , a : Optional[int] , a : Optional[int] , a : Union[str, Any] , a : List[Any] , a : Any=None , **a : Dict ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Optional[Any] = {'vision_model': vision_model, 'text_model': text_model} lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel.from_vision_text_pretrained(**a ) lowerCAmelCase__ : Union[str, Any] = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def _lowerCamelCase ( self : Any , a : Optional[int] , a : Optional[int] , a : Dict , a : Optional[int] , a : Optional[int]=None , **a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : int = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Dict = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : List[str] = model(input_ids=a , pixel_values=a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = output[0].numpy() with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(a ) lowerCAmelCase__ : Any = TFVisionTextDualEncoderModel.from_pretrained(a ) lowerCAmelCase__ : int = model(input_ids=a , pixel_values=a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = after_output[0].numpy() lowerCAmelCase__ : Optional[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(a , 1E-5 ) def _lowerCamelCase ( self : List[str] , a : Dict , a : Optional[int] , a : List[Any] , a : str , a : int=None , **a : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Any = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : str = model( input_ids=a , pixel_values=a , attention_mask=a , output_attentions=a ) lowerCAmelCase__ : Union[str, Any] = output.vision_model_output.attentions self.assertEqual(len(a ) , vision_config.num_hidden_layers ) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCAmelCase__ : Optional[int] = to_atuple(vision_model.config.image_size ) lowerCAmelCase__ : Optional[Any] = to_atuple(vision_model.config.patch_size ) lowerCAmelCase__ : List[Any] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase__ : int = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCAmelCase__ : str = output.text_model_output.attentions self.assertEqual(len(a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _lowerCamelCase ( self : List[Any] , a : np.ndarray , a : np.ndarray , a : float ): '''simple docstring''' lowerCAmelCase__ : int = np.abs((a - b) ).max() self.assertLessEqual(a , a , f'''Difference between torch and flax is {diff} (>= {tol}).''' ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_model(**a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Any = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : str = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.prepare_config_and_inputs() self.check_save_load(**a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**a ) @slow def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = self.get_pretrained_model_and_inputs() lowerCAmelCase__ : List[Any] = model_a(**a ) lowerCAmelCase__ : Optional[int] = outputs[0].numpy() with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(a ) lowerCAmelCase__ : str = TFVisionTextDualEncoderModel.from_pretrained(a ) lowerCAmelCase__ : List[str] = model_a(**a ) lowerCAmelCase__ : int = after_outputs[0].numpy() lowerCAmelCase__ : List[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(a , 1E-5 ) @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-vit' , 'hf-internal-testing/tiny-random-bert' ) lowerCAmelCase__ : int = 13 lowerCAmelCase__ : List[Any] = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : int = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Optional[Any] = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : List[Any] = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : List[Any] , a : Dict , a : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = TFViTModel(a , name='vision_model' ) lowerCAmelCase__ : str = TFBertModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = TFViTModelTester(self ) lowerCAmelCase__ : Tuple = TFBertModelTester(self ) lowerCAmelCase__ : Optional[int] = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Union[str, Any] = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : int = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : str = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'Rocketknight1/tiny-random-deit-tf' , 'hf-internal-testing/tiny-random-roberta' ) lowerCAmelCase__ : Tuple = 13 lowerCAmelCase__ : Any = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : Dict = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Any = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : Tuple = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : str , a : Optional[Any] , a : Dict , a : Dict , a : Any , a : Any=None , **a : int ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Optional[int] = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : Any = model( input_ids=a , pixel_values=a , attention_mask=a , output_attentions=a ) lowerCAmelCase__ : Union[str, Any] = output.vision_model_output.attentions self.assertEqual(len(a ) , vision_config.num_hidden_layers ) # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) lowerCAmelCase__ : str = to_atuple(vision_model.config.image_size ) lowerCAmelCase__ : Union[str, Any] = to_atuple(vision_model.config.patch_size ) lowerCAmelCase__ : int = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase__ : int = num_patches + 2 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCAmelCase__ : List[str] = output.text_model_output.attentions self.assertEqual(len(a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _lowerCamelCase ( self : int , a : Optional[int] , a : int ): '''simple docstring''' lowerCAmelCase__ : Dict = TFDeiTModel(a , name='vision_model' ) lowerCAmelCase__ : List[Any] = TFRobertaModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = TFDeiTModelTester(self ) lowerCAmelCase__ : List[str] = TFRobertaModelTester(self ) lowerCAmelCase__ : str = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : List[Any] = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : List[str] = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : Any = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : int = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'Rocketknight1/tiny-random-clip-tf' , 'hf-internal-testing/tiny-random-bert' ) lowerCAmelCase__ : Dict = 13 lowerCAmelCase__ : str = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : List[Any] = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Union[str, Any] = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : Optional[int] = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : str , a : int , a : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = TFCLIPVisionModel(a , name='vision_model' ) lowerCAmelCase__ : List[str] = TFBertModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Any = TFCLIPVisionModelTester(self ) lowerCAmelCase__ : Union[str, Any] = TFBertModelTester(self ) lowerCAmelCase__ : Any = clip_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Any = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ : List[Any] = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : str = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_vision @require_tf class A__ ( unittest.TestCase ): @slow def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel.from_pretrained( 'clip-italian/clip-italian' , logit_scale_init_value=1.0 , from_pt=a ) lowerCAmelCase__ : List[Any] = VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian' ) lowerCAmelCase__ : int = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) lowerCAmelCase__ : Any = processor( text=['una foto di un gatto', 'una foto di un cane'] , images=a , padding=a , return_tensors='np' ) lowerCAmelCase__ : Union[str, Any] = model(**a ) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) ) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) lowerCAmelCase__ : List[str] = np.array([[1.2_2_8_4_7_2_7, 0.3_1_0_4_1_2_2]] ) self.assertTrue(np.allclose(outputs.logits_per_image.numpy() , a , atol=1E-3 ) )
307
0
from importlib import import_module from .logging import get_logger lowerCamelCase__ = get_logger(__name__) class A__ : def __init__( self : Dict , a : Optional[Any] , a : Any=None ): '''simple docstring''' lowerCAmelCase__ : List[str] = attrs or [] if module is not None: for key in module.__dict__: if key in attrs or not key.startswith('__' ): setattr(self , lowercase_ , getattr(lowercase_ , lowercase_ ) ) lowerCAmelCase__ : Dict = module._original_module if isinstance(lowercase_ , _PatchedModuleObj ) else module class A__ : lowercase = [] def __init__( self : Union[str, Any] , a : Optional[Any] , a : str , a : Dict , a : int=None ): '''simple docstring''' lowerCAmelCase__ : int = obj lowerCAmelCase__ : Optional[Any] = target lowerCAmelCase__ : Dict = new lowerCAmelCase__ : List[Any] = target.split('.' )[0] lowerCAmelCase__ : Union[str, Any] = {} lowerCAmelCase__ : List[Any] = attrs or [] def __enter__( self : List[str] ): '''simple docstring''' *lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = self.target.split('.' ) # Patch modules: # it's used to patch attributes of submodules like "os.path.join"; # in this case we need to patch "os" and "os.path" for i in range(len(lowercase_ ) ): try: lowerCAmelCase__ : Tuple = import_module('.'.join(submodules[: i + 1] ) ) except ModuleNotFoundError: continue # We iterate over all the globals in self.obj in case we find "os" or "os.path" for attr in self.obj.__dir__(): lowerCAmelCase__ : List[Any] = getattr(self.obj , lowercase_ ) # We don't check for the name of the global, but rather if its value *is* "os" or "os.path". # This allows to patch renamed modules like "from os import path as ospath". if obj_attr is submodule or ( (isinstance(lowercase_ , _PatchedModuleObj ) and obj_attr._original_module is submodule) ): lowerCAmelCase__ : Any = obj_attr # patch at top level setattr(self.obj , lowercase_ , _PatchedModuleObj(lowercase_ , attrs=self.attrs ) ) lowerCAmelCase__ : Optional[Any] = getattr(self.obj , lowercase_ ) # construct lower levels patches for key in submodules[i + 1 :]: setattr(lowercase_ , lowercase_ , _PatchedModuleObj(getattr(lowercase_ , lowercase_ , lowercase_ ) , attrs=self.attrs ) ) lowerCAmelCase__ : Union[str, Any] = getattr(lowercase_ , lowercase_ ) # finally set the target attribute setattr(lowercase_ , lowercase_ , self.new ) # Patch attribute itself: # it's used for builtins like "open", # and also to patch "os.path.join" we may also need to patch "join" # itself if it was imported as "from os.path import join". if submodules: # if it's an attribute of a submodule like "os.path.join" try: lowerCAmelCase__ : Any = getattr(import_module('.'.join(lowercase_ ) ) , lowercase_ ) except (AttributeError, ModuleNotFoundError): return # We iterate over all the globals in self.obj in case we find "os.path.join" for attr in self.obj.__dir__(): # We don't check for the name of the global, but rather if its value *is* "os.path.join". # This allows to patch renamed attributes like "from os.path import join as pjoin". if getattr(self.obj , lowercase_ ) is attr_value: lowerCAmelCase__ : Optional[int] = getattr(self.obj , lowercase_ ) setattr(self.obj , lowercase_ , self.new ) elif target_attr in globals()["__builtins__"]: # if it'a s builtin like "open" lowerCAmelCase__ : List[str] = globals()['__builtins__'][target_attr] setattr(self.obj , lowercase_ , self.new ) else: raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.''' ) def __exit__( self : List[str] , *a : Tuple ): '''simple docstring''' for attr in list(self.original ): setattr(self.obj , lowercase_ , self.original.pop(lowercase_ ) ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' self.__enter__() self._active_patches.append(self ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' try: self._active_patches.remove(self ) except ValueError: # If the patch hasn't been started this will fail return None return self.__exit__()
356
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) lowerCamelCase__ = logging.getLogger(__name__) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : Dict = np.argmax(SCREAMING_SNAKE_CASE_ , axis=1 ) return np.sum(outputs == labels ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: with open(SCREAMING_SNAKE_CASE_ , encoding='utf_8' ) as f: lowerCAmelCase__ : Dict = csv.reader(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = [] next(SCREAMING_SNAKE_CASE_ ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE_ ): output.append((' '.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: lowerCAmelCase__ : Dict = [] for dataset in encoded_datasets: lowerCAmelCase__ : List[str] = len(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase__ : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase__ : List[Any] = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) lowerCAmelCase__ : Tuple = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase__ : Any = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase__ : Optional[Any] = with_conta lowerCAmelCase__ : List[str] = with_conta lowerCAmelCase__ : List[Any] = len(SCREAMING_SNAKE_CASE_ ) - 1 lowerCAmelCase__ : Tuple = len(SCREAMING_SNAKE_CASE_ ) - 1 lowerCAmelCase__ : Tuple = with_conta lowerCAmelCase__ : Optional[int] = with_conta lowerCAmelCase__ : Optional[int] = mc_label lowerCAmelCase__ : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE_ ) for t in all_inputs ) ) return tensor_datasets def lowerCAmelCase__ ( ) -> int: lowerCAmelCase__ : int = argparse.ArgumentParser() parser.add_argument('--model_name' , type=SCREAMING_SNAKE_CASE_ , default='openai-gpt' , help='pretrained model name' ) parser.add_argument('--do_train' , action='store_true' , help='Whether to run training.' ) parser.add_argument('--do_eval' , action='store_true' , help='Whether to run eval on the dev set.' ) parser.add_argument( '--output_dir' , default=SCREAMING_SNAKE_CASE_ , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , help='The output directory where the model predictions and checkpoints will be written.' , ) parser.add_argument('--train_dataset' , type=SCREAMING_SNAKE_CASE_ , default='' ) parser.add_argument('--eval_dataset' , type=SCREAMING_SNAKE_CASE_ , default='' ) parser.add_argument('--seed' , type=SCREAMING_SNAKE_CASE_ , default=42 ) parser.add_argument('--num_train_epochs' , type=SCREAMING_SNAKE_CASE_ , default=3 ) parser.add_argument('--train_batch_size' , type=SCREAMING_SNAKE_CASE_ , default=8 ) parser.add_argument('--eval_batch_size' , type=SCREAMING_SNAKE_CASE_ , default=16 ) parser.add_argument('--adam_epsilon' , default=1e-8 , type=SCREAMING_SNAKE_CASE_ , help='Epsilon for Adam optimizer.' ) parser.add_argument('--max_grad_norm' , type=SCREAMING_SNAKE_CASE_ , default=1 ) parser.add_argument( '--max_steps' , default=-1 , type=SCREAMING_SNAKE_CASE_ , help=( 'If > 0: set total number of training steps to perform. Override num_train_epochs.' ) , ) parser.add_argument( '--gradient_accumulation_steps' , type=SCREAMING_SNAKE_CASE_ , default=1 , help='Number of updates steps to accumulate before performing a backward/update pass.' , ) parser.add_argument('--learning_rate' , type=SCREAMING_SNAKE_CASE_ , default=6.25e-5 ) parser.add_argument('--warmup_steps' , default=0 , type=SCREAMING_SNAKE_CASE_ , help='Linear warmup over warmup_steps.' ) parser.add_argument('--lr_schedule' , type=SCREAMING_SNAKE_CASE_ , default='warmup_linear' ) parser.add_argument('--weight_decay' , type=SCREAMING_SNAKE_CASE_ , default=0.01 ) parser.add_argument('--lm_coef' , type=SCREAMING_SNAKE_CASE_ , default=0.9 ) parser.add_argument('--n_valid' , type=SCREAMING_SNAKE_CASE_ , default=374 ) parser.add_argument('--server_ip' , type=SCREAMING_SNAKE_CASE_ , default='' , help='Can be used for distant debugging.' ) parser.add_argument('--server_port' , type=SCREAMING_SNAKE_CASE_ , default='' , help='Can be used for distant debugging.' ) lowerCAmelCase__ : List[str] = parser.parse_args() print(SCREAMING_SNAKE_CASE_ ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('Waiting for debugger attach' ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=SCREAMING_SNAKE_CASE_ ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) lowerCAmelCase__ : str = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) lowerCAmelCase__ : Dict = torch.cuda.device_count() logger.info('device: {}, n_gpu {}'.format(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) if not args.do_train and not args.do_eval: raise ValueError('At least one of `do_train` or `do_eval` must be True.' ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset lowerCAmelCase__ : Union[str, Any] = ['_start_', '_delimiter_', '_classify_'] lowerCAmelCase__ : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE_ ) ) model.to(SCREAMING_SNAKE_CASE_ ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE_ ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) ) elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE_ ) for o in obj] logger.info('Encoding dataset...' ) lowerCAmelCase__ : List[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase__ : str = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase__ : Union[str, Any] = (train_dataset, eval_dataset) lowerCAmelCase__ : List[str] = tokenize_and_encode(SCREAMING_SNAKE_CASE_ ) # Compute the max input length for the Transformer lowerCAmelCase__ : Union[str, Any] = model.config.n_positions // 2 - 2 lowerCAmelCase__ : Tuple = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) lowerCAmelCase__ : Dict = min(SCREAMING_SNAKE_CASE_ , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase__ : int = pre_process_datasets(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase__ : str = TensorDataset(*SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = RandomSampler(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = DataLoader(SCREAMING_SNAKE_CASE_ , sampler=SCREAMING_SNAKE_CASE_ , batch_size=args.train_batch_size ) lowerCAmelCase__ : Optional[Any] = TensorDataset(*SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = SequentialSampler(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = DataLoader(SCREAMING_SNAKE_CASE_ , sampler=SCREAMING_SNAKE_CASE_ , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase__ : Union[str, Any] = args.max_steps lowerCAmelCase__ : int = args.max_steps // (len(SCREAMING_SNAKE_CASE_ ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase__ : Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase__ : Optional[int] = list(model.named_parameters() ) lowerCAmelCase__ : Tuple = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] lowerCAmelCase__ : str = [ { 'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], 'weight_decay': args.weight_decay, }, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], 'weight_decay': 0.0}, ] lowerCAmelCase__ : Union[str, Any] = AdamW(SCREAMING_SNAKE_CASE_ , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase__ : int = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE_ , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE_ ) if args.do_train: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc='Epoch' ): lowerCAmelCase__ : str = 0 lowerCAmelCase__ : int = 0 lowerCAmelCase__ : str = tqdm(SCREAMING_SNAKE_CASE_ , desc='Training' ) for step, batch in enumerate(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Union[str, Any] = tuple(t.to(SCREAMING_SNAKE_CASE_ ) for t in batch ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Any = batch lowerCAmelCase__ : Tuple = model(SCREAMING_SNAKE_CASE_ , mc_token_ids=SCREAMING_SNAKE_CASE_ , lm_labels=SCREAMING_SNAKE_CASE_ , mc_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : str = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase__ : Optional[int] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase__ : List[str] = 'Training loss: {:.2e} lr: {:.2e}'.format(SCREAMING_SNAKE_CASE_ , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase__ : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE_ , 'module' ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase__ : List[str] = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : List[str] = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE_ ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE_ ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase__ : Dict = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase__ : List[Any] = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE_ ) if args.do_eval: model.eval() lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = 0, 0 lowerCAmelCase__ , lowerCAmelCase__ : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE_ , desc='Evaluating' ): lowerCAmelCase__ : str = tuple(t.to(SCREAMING_SNAKE_CASE_ ) for t in batch ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Tuple = batch with torch.no_grad(): lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : List[str] = model( SCREAMING_SNAKE_CASE_ , mc_token_ids=SCREAMING_SNAKE_CASE_ , lm_labels=SCREAMING_SNAKE_CASE_ , mc_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = mc_logits.detach().cpu().numpy() lowerCAmelCase__ : List[Any] = mc_labels.to('cpu' ).numpy() lowerCAmelCase__ : str = accuracy(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase__ : Optional[int] = eval_loss / nb_eval_steps lowerCAmelCase__ : Any = eval_accuracy / nb_eval_examples lowerCAmelCase__ : Union[str, Any] = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase__ : Tuple = {'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'train_loss': train_loss} lowerCAmelCase__ : Dict = os.path.join(args.output_dir , 'eval_results.txt' ) with open(SCREAMING_SNAKE_CASE_ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key in sorted(result.keys() ): logger.info(' %s = %s' , SCREAMING_SNAKE_CASE_ , str(result[key] ) ) writer.write('%s = %s\n' % (key, str(result[key] )) ) if __name__ == "__main__": main()
307
0
from __future__ import annotations from random import random class A__ : def __init__( self : Any , a : Union[str, Any] = None ): '''simple docstring''' lowerCAmelCase__ : Dict = value lowerCAmelCase__ : Optional[int] = random() lowerCAmelCase__ : List[Any] = None lowerCAmelCase__ : str = None def __repr__( self : Union[str, Any] ): '''simple docstring''' from pprint import pformat if self.left is None and self.right is None: return f'''\'{self.value}: {self.prior:.5}\'''' else: return pformat( {f'''{self.value}: {self.prior:.5}''': (self.left, self.right)} , indent=1 ) def __str__( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[Any] = str(self.value ) + ' ' lowerCAmelCase__ : int = str(self.left or '' ) lowerCAmelCase__ : str = str(self.right or '' ) return value + left + right def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> tuple[Node | None, Node | None]: if root is None: # None tree is split into 2 Nones return None, None elif root.value is None: return None, None else: if value < root.value: lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = split(root.left , lowercase_ ) return left, root else: lowerCAmelCase__ , lowerCAmelCase__ : Tuple = split(root.right , lowercase_ ) return root, right def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Node | None: if (not left) or (not right): # If one node is None, return the other return left or right elif left.prior < right.prior: lowerCAmelCase__ : Tuple = merge(left.right , lowercase_ ) return left else: lowerCAmelCase__ : Optional[Any] = merge(lowercase_ , right.left ) return right def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Node | None: lowerCAmelCase__ : Optional[Any] = Node(lowercase_ ) lowerCAmelCase__ , lowerCAmelCase__ : Any = split(lowercase_ , lowercase_ ) return merge(merge(lowercase_ , lowercase_ ) , lowercase_ ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Node | None: lowerCAmelCase__ , lowerCAmelCase__ : List[str] = split(lowercase_ , value - 1 ) lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = split(lowercase_ , lowercase_ ) return merge(lowercase_ , lowercase_ ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> None: if not root: # None return else: inorder(root.left ) print(root.value , end=',' ) inorder(root.right ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Node | None: for arg in args.split(): if arg[0] == "+": lowerCAmelCase__ : List[str] = insert(lowercase_ , int(arg[1:] ) ) elif arg[0] == "-": lowerCAmelCase__ : int = erase(lowercase_ , int(arg[1:] ) ) else: print('Unknown command' ) return root def lowerCAmelCase__ ( ) -> None: lowerCAmelCase__ : List[Any] = None print( 'enter numbers to create a tree, + value to add value into treap, ' '- value to erase all nodes with value. \'q\' to quit. ' ) lowerCAmelCase__ : Optional[int] = input() while args != "q": lowerCAmelCase__ : Union[str, Any] = interact_treap(lowercase_ , lowercase_ ) print(lowercase_ ) lowerCAmelCase__ : Optional[Any] = input() print('good by!' ) if __name__ == "__main__": import doctest doctest.testmod() main()
357
import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers lowerCamelCase__ = """python tqdm regex requests packaging filelock numpy tokenizers""".split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append("""dataclasses""") if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append("""importlib_metadata""") for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F"""can't find {pkg} in {deps.keys()}, check dependency_versions_table.py""") def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> int: require_version(deps[pkg] , SCREAMING_SNAKE_CASE_ )
307
0
import warnings from ...utils import is_sklearn_available, requires_backends if is_sklearn_available(): from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef lowerCamelCase__ = ( '''This metric will be removed from the library soon, metrics should be handled with the 🤗 Evaluate ''' '''library. You can have a look at this example script for pointers: ''' '''https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py''' ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: warnings.warn(A_ , A_ ) requires_backends(A_ , 'sklearn' ) return (preds == labels).mean() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: warnings.warn(A_ , A_ ) requires_backends(A_ , 'sklearn' ) lowerCAmelCase__ : Dict = simple_accuracy(A_ , A_ ) lowerCAmelCase__ : Dict = fa_score(y_true=A_ , y_pred=A_ ) return { "acc": acc, "f1": fa, "acc_and_f1": (acc + fa) / 2, } def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: warnings.warn(A_ , A_ ) requires_backends(A_ , 'sklearn' ) lowerCAmelCase__ : Tuple = pearsonr(A_ , A_ )[0] lowerCAmelCase__ : int = spearmanr(A_ , A_ )[0] return { "pearson": pearson_corr, "spearmanr": spearman_corr, "corr": (pearson_corr + spearman_corr) / 2, } def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: warnings.warn(A_ , A_ ) requires_backends(A_ , 'sklearn' ) assert len(A_ ) == len(A_ ), F'''Predictions and labels have mismatched lengths {len(A_ )} and {len(A_ )}''' if task_name == "cola": return {"mcc": matthews_corrcoef(A_ , A_ )} elif task_name == "sst-2": return {"acc": simple_accuracy(A_ , A_ )} elif task_name == "mrpc": return acc_and_fa(A_ , A_ ) elif task_name == "sts-b": return pearson_and_spearman(A_ , A_ ) elif task_name == "qqp": return acc_and_fa(A_ , A_ ) elif task_name == "mnli": return {"mnli/acc": simple_accuracy(A_ , A_ )} elif task_name == "mnli-mm": return {"mnli-mm/acc": simple_accuracy(A_ , A_ )} elif task_name == "qnli": return {"acc": simple_accuracy(A_ , A_ )} elif task_name == "rte": return {"acc": simple_accuracy(A_ , A_ )} elif task_name == "wnli": return {"acc": simple_accuracy(A_ , A_ )} elif task_name == "hans": return {"acc": simple_accuracy(A_ , A_ )} else: raise KeyError(A_ ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: warnings.warn(A_ , A_ ) requires_backends(A_ , 'sklearn' ) if len(A_ ) != len(A_ ): raise ValueError(F'''Predictions and labels have mismatched lengths {len(A_ )} and {len(A_ )}''' ) if task_name == "xnli": return {"acc": simple_accuracy(A_ , A_ )} else: raise KeyError(A_ )
358
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class A__ ( __magic_name__ ): lowercase = (DPMSolverSDEScheduler,) lowercase = 10 def _lowerCamelCase ( self : Optional[int] , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = { 'num_train_timesteps': 1_100, 'beta_start': 0.0_0_0_1, 'beta_end': 0.0_2, 'beta_schedule': 'linear', 'noise_sampler_seed': 0, } config.update(**a ) return config def _lowerCamelCase ( self : Tuple ): '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=a ) def _lowerCamelCase ( self : int ): '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=a , beta_end=a ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=a ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = self.scheduler_classes[0] lowerCAmelCase__ : Tuple = self.get_scheduler_config() lowerCAmelCase__ : List[Any] = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Dict = self.dummy_model() lowerCAmelCase__ : int = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : int = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : List[Any] = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : int = scheduler.step(a , a , a ) lowerCAmelCase__ : Any = output.prev_sample lowerCAmelCase__ : List[Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Optional[int] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_7_8_2_1_0_4_4_9_2_1_8_7_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_7_8_7_0_5_9_6_4_5_6_5_2_7_7 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_2_1_1_1_8_1_6_4_0_6 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_6_8_9_2_2_9_9_6_5_2 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.scheduler_classes[0] lowerCAmelCase__ : List[str] = self.get_scheduler_config(prediction_type='v_prediction' ) lowerCAmelCase__ : Any = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Optional[int] = self.dummy_model() lowerCAmelCase__ : Union[str, Any] = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : Any = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : str = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : Dict = scheduler.step(a , a , a ) lowerCAmelCase__ : Tuple = output.prev_sample lowerCAmelCase__ : int = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Union[str, Any] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_2_4.7_7_1_4_9_2_0_0_4_3_9_4_5_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_2_2_6_2_8_9_0_1_4_8_1_6_2_8_4 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_2_8.1_6_6_3_3_6_0_5_9_5_7_0_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_6_8_8_3_2_6_0_0_1_1_6_7_2_9_7 ) < 1E-3 else: assert abs(result_sum.item() - 1_1_9.8_4_8_7_5_4_8_8_2_8_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.1_5_6_0_5_3_0_6_6_2_5_3_6_6_2_1 ) < 1E-3 def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.scheduler_classes[0] lowerCAmelCase__ : Optional[int] = self.get_scheduler_config() lowerCAmelCase__ : int = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : Tuple = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: lowerCAmelCase__ : Dict = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : Optional[int] = model(a , a ) lowerCAmelCase__ : Tuple = scheduler.step(a , a , a ) lowerCAmelCase__ : Dict = output.prev_sample lowerCAmelCase__ : Union[str, Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Dict = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_6_9_5_7_3_9_7_4_6_0_9_3_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_8_0_5_9_3_4_6_0_7_9_8_2_6_3_5 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_3_6_3_7_6_9_5_3_1_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_8_3_8_2_4_1_5_7_7_1 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.scheduler_classes[0] lowerCAmelCase__ : Any = self.get_scheduler_config() lowerCAmelCase__ : Any = scheduler_class(**a , use_karras_sigmas=a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : str = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma lowerCAmelCase__ : str = sample.to(a ) for t in scheduler.timesteps: lowerCAmelCase__ : Any = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : int = model(a , a ) lowerCAmelCase__ : Union[str, Any] = scheduler.step(a , a , a ) lowerCAmelCase__ : Union[str, Any] = output.prev_sample lowerCAmelCase__ : Optional[int] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Any = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_7_6.6_6_9_7_4_1_3_5_7_4_2_1_8_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_7.6_3_6_5_3_5_6_4_4_5_3_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 else: assert abs(result_sum.item() - 1_7_0.3_1_3_5_2_2_3_3_8_8_6_7_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2
307
0
from bisect import bisect from itertools import accumulate def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: lowerCAmelCase__ : Optional[Any] = sorted(zip(_lowercase , _lowercase ) , key=lambda SCREAMING_SNAKE_CASE_ : x[0] / x[1] , reverse=_lowercase ) lowerCAmelCase__ , lowerCAmelCase__ : str = [i[0] for i in r], [i[1] for i in r] lowerCAmelCase__ : Optional[Any] = list(accumulate(_lowercase ) ) lowerCAmelCase__ : int = bisect(_lowercase , _lowercase ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
359
import os import string import sys lowerCamelCase__ = 1 << 8 lowerCamelCase__ = { """tab""": ord("""\t"""), """newline""": ord("""\r"""), """esc""": 27, """up""": 65 + ARROW_KEY_FLAG, """down""": 66 + ARROW_KEY_FLAG, """right""": 67 + ARROW_KEY_FLAG, """left""": 68 + ARROW_KEY_FLAG, """mod_int""": 91, """undefined""": sys.maxsize, """interrupt""": 3, """insert""": 50, """delete""": 51, """pg_up""": 53, """pg_down""": 54, } lowerCamelCase__ = KEYMAP["""up"""] lowerCamelCase__ = KEYMAP["""left"""] if sys.platform == "win32": lowerCamelCase__ = [] lowerCamelCase__ = { b"""\xe0H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\x00H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\xe0P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\x00P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\xe0M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\x00M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\xe0K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, b"""\x00K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, } for i in range(10): lowerCamelCase__ = ord(str(i)) def lowerCAmelCase__ ( ) -> Dict: if os.name == "nt": import msvcrt lowerCAmelCase__ : Dict = 'mbcs' # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(SCREAMING_SNAKE_CASE_ ) == 0: # Read the keystroke lowerCAmelCase__ : Optional[Any] = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): lowerCAmelCase__ : Dict = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: lowerCAmelCase__ : Dict = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP['mod_int'] ) ) WIN_CH_BUFFER.append(SCREAMING_SNAKE_CASE_ ) if ord(SCREAMING_SNAKE_CASE_ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) lowerCAmelCase__ : Dict = chr(KEYMAP['esc'] ) except KeyError: lowerCAmelCase__ : Dict = cha[1] else: lowerCAmelCase__ : List[Any] = ch.decode(SCREAMING_SNAKE_CASE_ ) else: lowerCAmelCase__ : Tuple = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty lowerCAmelCase__ : Tuple = sys.stdin.fileno() lowerCAmelCase__ : Any = termios.tcgetattr(SCREAMING_SNAKE_CASE_ ) try: tty.setraw(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = sys.stdin.read(1 ) finally: termios.tcsetattr(SCREAMING_SNAKE_CASE_ , termios.TCSADRAIN , SCREAMING_SNAKE_CASE_ ) return ch def lowerCAmelCase__ ( ) -> Union[str, Any]: lowerCAmelCase__ : Any = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["esc"]: lowerCAmelCase__ : Union[str, Any] = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["mod_int"]: lowerCAmelCase__ : str = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(SCREAMING_SNAKE_CASE_ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(SCREAMING_SNAKE_CASE_ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
307
0
"""simple docstring""" from collections.abc import Sequence def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = None ) -> Tuple: if nums is None or not nums: raise ValueError('Input sequence should not be empty' ) lowerCAmelCase__ : Optional[Any] = nums[0] for i in range(1 , len(SCREAMING_SNAKE_CASE_ ) ): lowerCAmelCase__ : int = nums[i] lowerCAmelCase__ : Any = max(SCREAMING_SNAKE_CASE_ , ans + num , SCREAMING_SNAKE_CASE_ ) return ans if __name__ == "__main__": import doctest doctest.testmod() # Try on a sample input from the user lowerCamelCase__ = int(input("""Enter number of elements : """).strip()) lowerCamelCase__ = list(map(int, input("""\nEnter the numbers : """).strip().split()))[:n] print(max_subsequence_sum(array))
360
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: return "".join([hex(SCREAMING_SNAKE_CASE_ )[2:].zfill(2 ).upper() for byte in list(SCREAMING_SNAKE_CASE_ )] ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> bytes: # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(SCREAMING_SNAKE_CASE_ ) % 2) != 0: raise ValueError( 'Base16 encoded data is invalid:\nData does not have an even number of hex digits.' ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(SCREAMING_SNAKE_CASE_ ) <= set('0123456789ABCDEF' ): raise ValueError( 'Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.' ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
307
0
from dataclasses import dataclass from typing import Optional, Tuple, Union import flax import jax import jax.numpy as jnp from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils_flax import ( CommonSchedulerState, FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, add_noise_common, get_velocity_common, ) @flax.struct.dataclass class A__ : lowercase = 42 # setable values lowercase = 42 lowercase = 42 lowercase = None @classmethod def _lowerCamelCase ( cls : List[str] , a : List[str] , a : Any , a : List[str] ): '''simple docstring''' return cls(common=lowerCamelCase_ , init_noise_sigma=lowerCamelCase_ , timesteps=lowerCamelCase_ ) @dataclass class A__ ( __magic_name__ ): lowercase = 42 class A__ ( __magic_name__ , __magic_name__ ): lowercase = [e.name for e in FlaxKarrasDiffusionSchedulers] lowercase = 42 @property def _lowerCamelCase ( self : Dict ): '''simple docstring''' return True @register_to_config def __init__( self : str , a : List[Any] = 1_000 , a : Dict = 0.0_0_0_1 , a : Optional[int] = 0.0_2 , a : int = "linear" , a : Any = None , a : Union[str, Any] = "fixed_small" , a : str = True , a : List[str] = "epsilon" , a : int = jnp.floataa , ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = dtype def _lowerCamelCase ( self : Optional[Any] , a : Optional[int] = None ): '''simple docstring''' if common is None: lowerCAmelCase__ : int = CommonSchedulerState.create(self ) # standard deviation of the initial noise distribution lowerCAmelCase__ : str = jnp.array(1.0 , dtype=self.dtype ) lowerCAmelCase__ : Optional[Any] = jnp.arange(0 , self.config.num_train_timesteps ).round()[::-1] return DDPMSchedulerState.create( common=lowerCamelCase_ , init_noise_sigma=lowerCamelCase_ , timesteps=lowerCamelCase_ , ) def _lowerCamelCase ( self : str , a : str , a : Tuple , a : int = None ): '''simple docstring''' return sample def _lowerCamelCase ( self : Dict , a : Union[str, Any] , a : Any , a : Dict = () ): '''simple docstring''' lowerCAmelCase__ : int = self.config.num_train_timesteps // num_inference_steps # creates integer timesteps by multiplying by ratio # rounding to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : List[str] = (jnp.arange(0 , lowerCamelCase_ ) * step_ratio).round()[::-1] return state.replace( num_inference_steps=lowerCamelCase_ , timesteps=lowerCamelCase_ , ) def _lowerCamelCase ( self : Tuple , a : int , a : Union[str, Any] , a : Optional[int]=None , a : List[str]=None ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = state.common.alphas_cumprod[t] lowerCAmelCase__ : Optional[Any] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample lowerCAmelCase__ : int = (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * state.common.betas[t] if variance_type is None: lowerCAmelCase__ : List[str] = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small": lowerCAmelCase__ : Dict = jnp.clip(lowerCamelCase_ , a_min=1E-20 ) # for rl-diffuser https://arxiv.org/abs/2205.09991 elif variance_type == "fixed_small_log": lowerCAmelCase__ : Any = jnp.log(jnp.clip(lowerCamelCase_ , a_min=1E-20 ) ) elif variance_type == "fixed_large": lowerCAmelCase__ : List[str] = state.common.betas[t] elif variance_type == "fixed_large_log": # Glide max_log lowerCAmelCase__ : Any = jnp.log(state.common.betas[t] ) elif variance_type == "learned": return predicted_variance elif variance_type == "learned_range": lowerCAmelCase__ : Optional[int] = variance lowerCAmelCase__ : List[Any] = state.common.betas[t] lowerCAmelCase__ : Optional[int] = (predicted_variance + 1) / 2 lowerCAmelCase__ : str = frac * max_log + (1 - frac) * min_log return variance def _lowerCamelCase ( self : Dict , a : Optional[Any] , a : Union[str, Any] , a : Union[str, Any] , a : Dict , a : List[str] = None , a : List[str] = True , ): '''simple docstring''' lowerCAmelCase__ : Tuple = timestep if key is None: lowerCAmelCase__ : int = jax.random.PRNGKey(0 ) if model_output.shape[1] == sample.shape[1] * 2 and self.config.variance_type in ["learned", "learned_range"]: lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = jnp.split(lowerCamelCase_ , sample.shape[1] , axis=1 ) else: lowerCAmelCase__ : int = None # 1. compute alphas, betas lowerCAmelCase__ : Union[str, Any] = state.common.alphas_cumprod[t] lowerCAmelCase__ : Any = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) lowerCAmelCase__ : Tuple = 1 - alpha_prod_t lowerCAmelCase__ : Tuple = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": lowerCAmelCase__ : Tuple = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": lowerCAmelCase__ : int = model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase__ : Optional[int] = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output else: raise ValueError( f'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample` ''' ' for the FlaxDDPMScheduler.' ) # 3. Clip "predicted x_0" if self.config.clip_sample: lowerCAmelCase__ : int = jnp.clip(lowerCamelCase_ , -1 , 1 ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase__ : List[Any] = (alpha_prod_t_prev ** 0.5 * state.common.betas[t]) / beta_prod_t lowerCAmelCase__ : Union[str, Any] = state.common.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase__ : Dict = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise def random_variance(): lowerCAmelCase__ : int = jax.random.split(lowerCamelCase_ , num=1 ) lowerCAmelCase__ : List[str] = jax.random.normal(lowerCamelCase_ , shape=model_output.shape , dtype=self.dtype ) return (self._get_variance(lowerCamelCase_ , lowerCamelCase_ , predicted_variance=lowerCamelCase_ ) ** 0.5) * noise lowerCAmelCase__ : Tuple = jnp.where(t > 0 , random_variance() , jnp.zeros(model_output.shape , dtype=self.dtype ) ) lowerCAmelCase__ : Optional[Any] = pred_prev_sample + variance if not return_dict: return (pred_prev_sample, state) return FlaxDDPMSchedulerOutput(prev_sample=lowerCamelCase_ , state=lowerCamelCase_ ) def _lowerCamelCase ( self : List[str] , a : Any , a : Dict , a : Union[str, Any] , a : Tuple , ): '''simple docstring''' return add_noise_common(state.common , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def _lowerCamelCase ( self : Optional[Any] , a : int , a : Optional[int] , a : List[Any] , a : List[str] , ): '''simple docstring''' return get_velocity_common(state.common , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def __len__( self : Dict ): '''simple docstring''' return self.config.num_train_timesteps
361
from __future__ import annotations def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[list[int]]: lowerCAmelCase__ : list[list[int]] = [] create_all_state(1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , [] , SCREAMING_SNAKE_CASE_ ) return result def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) -> None: if level == 0: total_list.append(current_list[:] ) return for i in range(SCREAMING_SNAKE_CASE_ , total_number - level + 2 ): current_list.append(SCREAMING_SNAKE_CASE_ ) create_all_state(i + 1 , SCREAMING_SNAKE_CASE_ , level - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) current_list.pop() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> None: for i in total_list: print(*SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCamelCase__ = 4 lowerCamelCase__ = 2 lowerCamelCase__ = generate_all_combinations(n, k) print_all_state(total_list)
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> bool: return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(__lowerCAmelCase ) ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> bool: if index == len(__lowerCAmelCase ): return True # Recursive Step for i in range(__lowerCAmelCase ): if valid_coloring(graph[index] , __lowerCAmelCase , __lowerCAmelCase ): # Color current vertex lowerCAmelCase__ : Optional[Any] = i # Validate coloring if util_color(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , index + 1 ): return True # Backtrack lowerCAmelCase__ : int = -1 return False def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[int]: lowerCAmelCase__ : Union[str, Any] = [-1] * len(__lowerCAmelCase ) if util_color(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , 0 ): return colored_vertices return []
362
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class A__ ( unittest.TestCase ): @parameterized.expand([(None,), ('foo.json',)] ) def _lowerCamelCase ( self : Dict , a : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a , config_name=a ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(a , config_name=a ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 50 ) self.assertEqual(loaded_config.max_length , 20 ) self.assertEqual(loaded_config.max_time , a ) def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Dict = AutoConfig.from_pretrained('gpt2' ) lowerCAmelCase__ : Any = GenerationConfig.from_model_config(a ) lowerCAmelCase__ : Any = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(a , a ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : Dict = { 'max_new_tokens': 1_024, 'foo': 'bar', } lowerCAmelCase__ : List[Any] = copy.deepcopy(a ) lowerCAmelCase__ : Dict = generation_config.update(**a ) # update_kwargs was not modified (no side effects) self.assertEqual(a , a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_024 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(a , {'foo': 'bar'} ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : List[Any] = 'bar' with tempfile.TemporaryDirectory('test-generation-config' ) as tmp_dir: generation_config.save_pretrained(a ) lowerCAmelCase__ : List[Any] = GenerationConfig.from_pretrained(a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , 'bar' ) lowerCAmelCase__ : int = GenerationConfig.from_model_config(a ) assert not hasattr(a , 'foo' ) # no new kwargs should be initialized if from config def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , a ) self.assertEqual(default_config.num_beams , 1 ) lowerCAmelCase__ : List[Any] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , a ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(a , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class A__ ( unittest.TestCase ): @classmethod def _lowerCamelCase ( cls : int ): '''simple docstring''' lowerCAmelCase__ : List[str] = TOKEN HfFolder.save_token(a ) @classmethod def _lowerCamelCase ( cls : Optional[int] ): '''simple docstring''' try: delete_repo(token=cls._token , repo_id='test-generation-config' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-generation-config-org' ) except HTTPError: pass def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('test-generation-config' , use_auth_token=self._token ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='test-generation-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='test-generation-config' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('valid_org/test-generation-config-org' , use_auth_token=self._token ) lowerCAmelCase__ : Dict = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-generation-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='valid_org/test-generation-config-org' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : List[str] = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) )
307
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_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import GLPNImageProcessor class A__ ( unittest.TestCase ): def __init__( self : List[Any] , a : Tuple , a : Union[str, Any]=7 , a : List[str]=3 , a : List[str]=18 , a : List[Any]=30 , a : Optional[int]=400 , a : Tuple=True , a : str=32 , a : str=True , ): '''simple docstring''' lowerCAmelCase__ : str = parent lowerCAmelCase__ : str = batch_size lowerCAmelCase__ : Union[str, Any] = num_channels lowerCAmelCase__ : Optional[Any] = image_size lowerCAmelCase__ : int = min_resolution lowerCAmelCase__ : Optional[int] = max_resolution lowerCAmelCase__ : List[Any] = do_resize lowerCAmelCase__ : List[Any] = size_divisor lowerCAmelCase__ : Tuple = do_rescale def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return { "do_resize": self.do_resize, "size_divisor": self.size_divisor, "do_rescale": self.do_rescale, } @require_torch @require_vision class A__ ( a__ , unittest.TestCase ): lowercase = GLPNImageProcessor if is_vision_available() else None def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = GLPNImageProcessingTester(self ) @property def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCAmelCase__ , 'do_resize' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , 'size_divisor' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , 'resample' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , 'do_rescale' ) ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' pass def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowerCAmelCase__ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , Image.Image ) # Test not batched input (GLPNImageProcessor doesn't support batching) lowerCAmelCase__ : Optional[Any] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCAmelCase__ : int = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , numpify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , np.ndarray ) # Test not batched input (GLPNImageProcessor doesn't support batching) lowerCAmelCase__ : Union[str, Any] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : str = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCAmelCase__ : int = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , torchify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , torch.Tensor ) # Test not batched input (GLPNImageProcessor doesn't support batching) lowerCAmelCase__ : str = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )
363
import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import ( DiffusionPipeline, UnCLIPImageVariationPipeline, UnCLIPScheduler, UNetaDConditionModel, UNetaDModel, ) from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, load_image, require_torch_gpu, skip_mps from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = UnCLIPImageVariationPipeline lowercase = IMAGE_VARIATION_PARAMS - {'height', 'width', 'guidance_scale'} lowercase = IMAGE_VARIATION_BATCH_PARAMS lowercase = [ 'generator', 'return_dict', 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] lowercase = False @property def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : int ): '''simple docstring''' return self.time_input_dim @property def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return self.time_input_dim * 4 @property def _lowerCamelCase ( self : Any ): '''simple docstring''' return 100 @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) return tokenizer @property def _lowerCamelCase ( self : Any ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) return CLIPTextModelWithProjection(a ) @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : List[Any] = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) return CLIPVisionModelWithProjection(a ) @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Union[str, Any] = { 'clip_embeddings_dim': self.text_embedder_hidden_size, 'time_embed_dim': self.time_embed_dim, 'cross_attention_dim': self.cross_attention_dim, } lowerCAmelCase__ : Optional[Any] = UnCLIPTextProjModel(**a ) return model @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : str = { 'sample_size': 32, # RGB in channels 'in_channels': 3, # Out channels is double in channels because predicts mean and variance 'out_channels': 6, 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': 'identity', } lowerCAmelCase__ : str = UNetaDConditionModel(**a ) return model @property def _lowerCamelCase ( self : str ): '''simple docstring''' return { "sample_size": 64, "layers_per_block": 1, "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"), "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"), "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "in_channels": 6, "out_channels": 3, } @property def _lowerCamelCase ( self : str ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Any = UNetaDModel(**self.dummy_super_res_kwargs ) return model @property def _lowerCamelCase ( self : int ): '''simple docstring''' torch.manual_seed(1 ) lowerCAmelCase__ : List[str] = UNetaDModel(**self.dummy_super_res_kwargs ) return model def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.dummy_decoder lowerCAmelCase__ : Optional[int] = self.dummy_text_proj lowerCAmelCase__ : Any = self.dummy_text_encoder lowerCAmelCase__ : Any = self.dummy_tokenizer lowerCAmelCase__ : Any = self.dummy_super_res_first lowerCAmelCase__ : Optional[int] = self.dummy_super_res_last lowerCAmelCase__ : Dict = UnCLIPScheduler( variance_type='learned_range' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = UnCLIPScheduler( variance_type='fixed_small_log' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = CLIPImageProcessor(crop_size=32 , size=32 ) lowerCAmelCase__ : Optional[int] = self.dummy_image_encoder return { "decoder": decoder, "text_encoder": text_encoder, "tokenizer": tokenizer, "text_proj": text_proj, "feature_extractor": feature_extractor, "image_encoder": image_encoder, "super_res_first": super_res_first, "super_res_last": super_res_last, "decoder_scheduler": decoder_scheduler, "super_res_scheduler": super_res_scheduler, } def _lowerCamelCase ( self : Any , a : Dict , a : List[str]=0 , a : List[str]=True ): '''simple docstring''' lowerCAmelCase__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(a ) ).to(a ) if str(a ).startswith('mps' ): lowerCAmelCase__ : Optional[int] = torch.manual_seed(a ) else: lowerCAmelCase__ : str = torch.Generator(device=a ).manual_seed(a ) if pil_image: lowerCAmelCase__ : Optional[int] = input_image * 0.5 + 0.5 lowerCAmelCase__ : Dict = input_image.clamp(0 , 1 ) lowerCAmelCase__ : List[Any] = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() lowerCAmelCase__ : Union[str, Any] = DiffusionPipeline.numpy_to_pil(a )[0] return { "image": input_image, "generator": generator, "decoder_num_inference_steps": 2, "super_res_num_inference_steps": 2, "output_type": "np", } def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = 'cpu' lowerCAmelCase__ : Any = self.get_dummy_components() lowerCAmelCase__ : List[str] = self.pipeline_class(**a ) lowerCAmelCase__ : Dict = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : str = pipe(**a ) lowerCAmelCase__ : Optional[Any] = output.images lowerCAmelCase__ : str = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : List[str] = np.array( [ 0.9_9_9_7, 0.0_0_0_2, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_6_9, 0.0_0_2_3, 0.9_9_9_7, 0.9_9_6_9, 0.9_9_7_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 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = 'cpu' lowerCAmelCase__ : Dict = self.get_dummy_components() lowerCAmelCase__ : Optional[int] = self.pipeline_class(**a ) lowerCAmelCase__ : int = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = pipe(**a ) lowerCAmelCase__ : Union[str, Any] = output.images lowerCAmelCase__ : int = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : int = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Tuple = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : str = np.array([0.9_9_9_7, 0.0_0_0_3, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_7_0, 0.0_0_2_4, 0.9_9_9_7, 0.9_9_7_1, 0.9_9_7_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Tuple = 'cpu' lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : Tuple = self.pipeline_class(**a ) lowerCAmelCase__ : Union[str, Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Tuple = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = [ pipeline_inputs['image'], pipeline_inputs['image'], ] lowerCAmelCase__ : Optional[int] = pipe(**a ) lowerCAmelCase__ : Tuple = output.images lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Union[str, Any] = [ tuple_pipeline_inputs['image'], tuple_pipeline_inputs['image'], ] lowerCAmelCase__ : str = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Any = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (2, 64, 64, 3) lowerCAmelCase__ : Union[str, Any] = np.array( [ 0.9_9_9_7, 0.9_9_8_9, 0.0_0_0_8, 0.0_0_2_1, 0.9_9_6_0, 0.0_0_1_8, 0.0_0_1_4, 0.0_0_0_2, 0.9_9_3_3, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch.device('cpu' ) class A__ : lowercase = 1 lowerCAmelCase__ : Optional[Any] = self.get_dummy_components() lowerCAmelCase__ : Dict = self.pipeline_class(**a ) lowerCAmelCase__ : Optional[Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.Generator(device=a ).manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe.decoder.dtype lowerCAmelCase__ : Union[str, Any] = 1 lowerCAmelCase__ : str = ( batch_size, pipe.decoder.config.in_channels, pipe.decoder.config.sample_size, pipe.decoder.config.sample_size, ) lowerCAmelCase__ : List[Any] = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[str] = ( batch_size, pipe.super_res_first.config.in_channels // 2, pipe.super_res_first.config.sample_size, pipe.super_res_first.config.sample_size, ) lowerCAmelCase__ : Any = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , decoder_latents=a , super_res_latents=a ).images lowerCAmelCase__ : Optional[Any] = self.get_dummy_inputs(a , pil_image=a ) # Don't pass image, instead pass embedding lowerCAmelCase__ : Union[str, Any] = pipeline_inputs.pop('image' ) lowerCAmelCase__ : Union[str, Any] = pipe.image_encoder(a ).image_embeds lowerCAmelCase__ : List[Any] = pipe( **a , decoder_latents=a , super_res_latents=a , image_embeddings=a , ).images # make sure passing text embeddings manually is identical assert np.abs(img_out_a - img_out_a ).max() < 1E-4 @skip_mps def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device == 'cpu' # Check is relaxed because there is not a torch 2.0 sliced attention added kv processor lowerCAmelCase__ : int = 1E-2 self._test_attention_slicing_forward_pass( test_max_difference=a , expected_max_diff=a ) @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = torch_device == 'cpu' lowerCAmelCase__ : Any = True lowerCAmelCase__ : Optional[Any] = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] self._test_inference_batch_single_identical( test_max_difference=a , relax_max_difference=a , additional_params_copy_to_batched_inputs=a , ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] if torch_device == "mps": # TODO: MPS errors with larger batch sizes lowerCAmelCase__ : List[str] = [2, 3] self._test_inference_batch_consistent( batch_sizes=a , additional_params_copy_to_batched_inputs=a , ) else: self._test_inference_batch_consistent( additional_params_copy_to_batched_inputs=a ) @skip_mps def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent() @skip_mps def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_optional_components() @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Tuple ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/unclip/cat.png' ) lowerCAmelCase__ : List[Any] = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/unclip/karlo_v1_alpha_cat_variation_fp16.npy' ) lowerCAmelCase__ : Tuple = UnCLIPImageVariationPipeline.from_pretrained( 'kakaobrain/karlo-v1-alpha-image-variations' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Union[str, Any] = pipeline.to(a ) pipeline.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = torch.Generator(device='cpu' ).manual_seed(0 ) lowerCAmelCase__ : List[str] = pipeline( a , generator=a , output_type='np' , ) lowerCAmelCase__ : Union[str, Any] = output.images[0] assert image.shape == (256, 256, 3) assert_mean_pixel_difference(a , a , 15 )
307
0
import fire from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoTokenizer from utils import SeqaSeqDataset, pickle_save def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=1_024 , SCREAMING_SNAKE_CASE_=1_024 , SCREAMING_SNAKE_CASE_=False , **SCREAMING_SNAKE_CASE_ ) -> Any: lowerCAmelCase__ : Optional[Any] = AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = SeqaSeqDataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , type_path='train' , **SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = tok.pad_token_id def get_lens(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Tuple = tqdm( DataLoader(SCREAMING_SNAKE_CASE_ , batch_size=512 , num_workers=8 , shuffle=SCREAMING_SNAKE_CASE_ , collate_fn=ds.collate_fn ) , desc=str(ds.len_file ) , ) lowerCAmelCase__ : Tuple = [] for batch in dl: lowerCAmelCase__ : Union[str, Any] = batch['input_ids'].ne(SCREAMING_SNAKE_CASE_ ).sum(1 ).tolist() lowerCAmelCase__ : Optional[int] = batch['labels'].ne(SCREAMING_SNAKE_CASE_ ).sum(1 ).tolist() if consider_target: for src, tgt in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): max_lens.append(max(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) else: max_lens.extend(SCREAMING_SNAKE_CASE_ ) return max_lens lowerCAmelCase__ : str = get_lens(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = SeqaSeqDataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , type_path='val' , **SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = get_lens(SCREAMING_SNAKE_CASE_ ) pickle_save(SCREAMING_SNAKE_CASE_ , train_ds.len_file ) pickle_save(SCREAMING_SNAKE_CASE_ , val_ds.len_file ) if __name__ == "__main__": fire.Fire(save_len_file)
364
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: stooge(SCREAMING_SNAKE_CASE_ , 0 , len(SCREAMING_SNAKE_CASE_ ) - 1 ) return arr def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowerCAmelCase__ , lowerCAmelCase__ : Tuple = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowerCAmelCase__ : Union[str, Any] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) # Recursively sort last 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , i + t , (SCREAMING_SNAKE_CASE_) ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) if __name__ == "__main__": lowerCamelCase__ = input("""Enter numbers separated by a comma:\n""").strip() lowerCamelCase__ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
307
0
import copy 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 ..auto import CONFIG_MAPPING lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """microsoft/conditional-detr-resnet-50""": ( """https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json""" ), } class A__ ( __lowerCAmelCase ): lowercase = '''conditional_detr''' lowercase = ['''past_key_values'''] lowercase = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', } def __init__( self : Optional[int] , a : List[Any]=True , a : int=None , a : str=3 , a : Any=300 , a : Optional[Any]=6 , a : Tuple=2_048 , a : Optional[Any]=8 , a : Dict=6 , a : str=2_048 , a : List[Any]=8 , a : List[str]=0.0 , a : Any=0.0 , a : List[str]=True , a : List[str]="relu" , a : List[Any]=256 , a : int=0.1 , a : str=0.0 , a : Union[str, Any]=0.0 , a : Any=0.0_2 , a : List[Any]=1.0 , a : Optional[int]=False , a : Optional[Any]="sine" , a : Tuple="resnet50" , a : str=True , a : List[Any]=False , a : int=2 , a : Optional[Any]=5 , a : Tuple=2 , a : List[Any]=1 , a : str=1 , a : Tuple=2 , a : Union[str, Any]=5 , a : int=2 , a : List[Any]=0.2_5 , **a : List[Any] , ): 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.' ) lowerCAmelCase__ : str = CONFIG_MAPPING['resnet'](out_features=['stage4'] ) elif isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): lowerCAmelCase__ : Optional[int] = backbone_config.get('model_type' ) lowerCAmelCase__ : Tuple = CONFIG_MAPPING[backbone_model_type] lowerCAmelCase__ : Tuple = config_class.from_dict(lowerCAmelCase_ ) lowerCAmelCase__ : List[str] = use_timm_backbone lowerCAmelCase__ : str = backbone_config lowerCAmelCase__ : List[str] = num_channels lowerCAmelCase__ : str = num_queries lowerCAmelCase__ : int = d_model lowerCAmelCase__ : Any = encoder_ffn_dim lowerCAmelCase__ : List[str] = encoder_layers lowerCAmelCase__ : List[Any] = encoder_attention_heads lowerCAmelCase__ : Union[str, Any] = decoder_ffn_dim lowerCAmelCase__ : Dict = decoder_layers lowerCAmelCase__ : Dict = decoder_attention_heads lowerCAmelCase__ : Union[str, Any] = dropout lowerCAmelCase__ : Tuple = attention_dropout lowerCAmelCase__ : List[Any] = activation_dropout lowerCAmelCase__ : Dict = activation_function lowerCAmelCase__ : List[str] = init_std lowerCAmelCase__ : Optional[int] = init_xavier_std lowerCAmelCase__ : Union[str, Any] = encoder_layerdrop lowerCAmelCase__ : List[Any] = decoder_layerdrop lowerCAmelCase__ : Tuple = encoder_layers lowerCAmelCase__ : Optional[Any] = auxiliary_loss lowerCAmelCase__ : Any = position_embedding_type lowerCAmelCase__ : Optional[Any] = backbone lowerCAmelCase__ : List[str] = use_pretrained_backbone lowerCAmelCase__ : List[Any] = dilation # Hungarian matcher lowerCAmelCase__ : int = class_cost lowerCAmelCase__ : int = bbox_cost lowerCAmelCase__ : Dict = giou_cost # Loss coefficients lowerCAmelCase__ : List[Any] = mask_loss_coefficient lowerCAmelCase__ : Dict = dice_loss_coefficient lowerCAmelCase__ : Dict = cls_loss_coefficient lowerCAmelCase__ : int = bbox_loss_coefficient lowerCAmelCase__ : Union[str, Any] = giou_loss_coefficient lowerCAmelCase__ : Optional[Any] = focal_alpha super().__init__(is_encoder_decoder=lowerCAmelCase_ , **lowerCAmelCase_ ) @property def _lowerCamelCase ( self : List[str] ): return self.encoder_attention_heads @property def _lowerCamelCase ( self : Tuple ): return self.d_model def _lowerCamelCase ( self : str ): lowerCAmelCase__ : int = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: lowerCAmelCase__ : Optional[Any] = self.backbone_config.to_dict() lowerCAmelCase__ : Dict = self.__class__.model_type return output class A__ ( __lowerCAmelCase ): lowercase = version.parse('1.11' ) @property def _lowerCamelCase ( self : Optional[int] ): return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ('pixel_mask', {0: 'batch'}), ] ) @property def _lowerCamelCase ( self : Tuple ): return 1E-5 @property def _lowerCamelCase ( self : int ): return 12
365
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) lowerCamelCase__ = { """configuration_speech_to_text""": ["""SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Speech2TextConfig"""], """processing_speech_to_text""": ["""Speech2TextProcessor"""], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextTokenizer"""] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextFeatureExtractor"""] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFSpeech2TextForConditionalGeneration""", """TFSpeech2TextModel""", """TFSpeech2TextPreTrainedModel""", ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """Speech2TextForConditionalGeneration""", """Speech2TextModel""", """Speech2TextPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
from scipy.stats import pearsonr import datasets lowerCamelCase__ = '\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n' lowerCamelCase__ = '\nArgs:\n predictions (`list` of `int`): Predicted class labels, as returned by a model.\n references (`list` of `int`): Ground truth labels.\n return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n Example 1-A simple example using only predictions and references.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n\n Example 2-The same as Example 1, but that also returns the `p-value`.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n >>> print(sorted(list(results.keys())))\n [\'p-value\', \'pearsonr\']\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n >>> print(round(results[\'p-value\'], 2))\n 0.15\n' lowerCamelCase__ = '\n@article{2020SciPy-NMeth,\nauthor = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, Ilhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Antonio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\njournal = {Nature Methods},\nyear = {2020},\nvolume = {17},\npages = {261--272},\nadsurl = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A__ ( datasets.Metric ): def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('float' ), 'references': datasets.Value('float' ), } ) , reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html'] , ) def _lowerCamelCase ( self : Any , a : int , a : str , a : str=False ): '''simple docstring''' if return_pvalue: lowerCAmelCase__ : int = pearsonr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )[0] )}
366
import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = { 'task_specific_params': { 'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4}, 'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4}, 'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6}, } } lowerCAmelCase__ : int = { 'task_specific_params.summarization.length_penalty': 1.0, 'task_specific_params.summarization.max_length': 128, 'task_specific_params.summarization.min_length': 12, 'task_specific_params.summarization.num_beams': 4, 'task_specific_params.summarization_cnn.length_penalty': 2.0, 'task_specific_params.summarization_cnn.max_length': 142, 'task_specific_params.summarization_cnn.min_length': 56, 'task_specific_params.summarization_cnn.num_beams': 4, 'task_specific_params.summarization_xsum.length_penalty': 1.0, 'task_specific_params.summarization_xsum.max_length': 62, 'task_specific_params.summarization_xsum.min_length': 11, 'task_specific_params.summarization_xsum.num_beams': 6, } self.assertEqual(flatten_dict(a ) , a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(a ) , x.transpose() ) ) lowerCAmelCase__ : List[str] = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : int = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Dict = tf.constant(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : int = jnp.array(a ) self.assertTrue(np.allclose(transpose(a ) , np.asarray(transpose(a ) ) ) ) lowerCAmelCase__ : Any = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = jnp.array(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , np.asarray(transpose(a , axes=(1, 2, 0) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.reshape(a , (4, 3) ) ) ) lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.reshape(a , (12, 5) ) ) ) @require_torch def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : Dict = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[str] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.asarray(reshape(a , (4, 3) ) ) ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.asarray(reshape(a , (12, 5) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(a ) , np.squeeze(a ) ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.squeeze(a , axis=2 ) ) ) @require_torch def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : Optional[Any] = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : str = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a ) , np.asarray(squeeze(a ) ) ) ) lowerCAmelCase__ : str = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Optional[Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.asarray(squeeze(a , axis=2 ) ) ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.expand_dims(a , axis=1 ) ) ) @require_torch def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : str = np.random.randn(3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = np.random.randn(3 , 4 ) lowerCAmelCase__ : Tuple = jnp.array(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.asarray(expand_dims(a , axis=1 ) ) ) )
307
0
lowerCamelCase__ = {str(digit): digit**5 for digit in range(10)} def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> int: return sum(DIGITS_FIFTH_POWER[digit] for digit in str(SCREAMING_SNAKE_CASE__ ) ) def lowerCAmelCase__ ( ) -> int: return sum( number for number in range(1_000 , 1_000_000 ) if number == digits_fifth_powers_sum(SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": print(solution())
367
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position lowerCamelCase__ = """2.13.1""" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("""3.7"""): raise ImportWarning( """To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition.""" ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( """To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n""" """If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.""" ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip lowerCamelCase__ = concatenate_datasets lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadManager lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
307
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCamelCase__ = { "configuration_altclip": [ "ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "AltCLIPConfig", "AltCLIPTextConfig", "AltCLIPVisionConfig", ], "processing_altclip": ["AltCLIPProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ "ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "AltCLIPPreTrainedModel", "AltCLIPModel", "AltCLIPTextModel", "AltCLIPVisionModel", ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
368
import gc import unittest import numpy as np import torch from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = DanceDiffusionPipeline lowercase = UNCONDITIONAL_AUDIO_GENERATION_PARAMS lowercase = PipelineTesterMixin.required_optional_params - { 'callback', 'latents', 'callback_steps', 'output_type', 'num_images_per_prompt', } lowercase = UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS lowercase = False lowercase = False def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = UNetaDModel( block_out_channels=(32, 32, 64) , extra_in_channels=16 , sample_size=512 , sample_rate=16_000 , in_channels=2 , out_channels=2 , flip_sin_to_cos=a , use_timestep_embedding=a , time_embedding_type='fourier' , mid_block_type='UNetMidBlock1D' , down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D') , up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip') , ) lowerCAmelCase__ : Tuple = IPNDMScheduler() lowerCAmelCase__ : str = { 'unet': unet, 'scheduler': scheduler, } return components def _lowerCamelCase ( self : int , a : Dict , a : List[str]=0 ): '''simple docstring''' if str(a ).startswith('mps' ): lowerCAmelCase__ : Union[str, Any] = torch.manual_seed(a ) else: lowerCAmelCase__ : Optional[Any] = torch.Generator(device=a ).manual_seed(a ) lowerCAmelCase__ : Optional[Any] = { 'batch_size': 1, 'generator': generator, 'num_inference_steps': 4, } return inputs def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'cpu' # ensure determinism for the device-dependent torch.Generator lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : List[str] = DanceDiffusionPipeline(**a ) lowerCAmelCase__ : Any = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a ) lowerCAmelCase__ : List[Any] = pipe(**a ) lowerCAmelCase__ : List[str] = output.audios lowerCAmelCase__ : Optional[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, components["unet"].sample_size) lowerCAmelCase__ : List[Any] = np.array([-0.7_2_6_5, 1.0_0_0_0, -0.8_3_8_8, 0.1_1_7_5, 0.9_4_9_8, -1.0_0_0_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) @skip_mps def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return super().test_save_load_optional_components() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_attention_slicing_forward_pass() def _lowerCamelCase ( self : List[str] ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Dict ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device lowerCAmelCase__ : List[str] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' ) lowerCAmelCase__ : List[str] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : int = output.audios lowerCAmelCase__ : List[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : Dict = np.array([-0.0_1_9_2, -0.0_2_3_1, -0.0_3_1_8, -0.0_0_5_9, 0.0_0_0_2, -0.0_0_2_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = torch_device lowerCAmelCase__ : List[Any] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Optional[int] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : str = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : str = output.audios lowerCAmelCase__ : Tuple = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : int = np.array([-0.0_3_6_7, -0.0_4_8_8, -0.0_7_7_1, -0.0_5_2_5, -0.0_4_4_4, -0.0_3_4_1] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2
307
0
from __future__ import annotations import typing from collections.abc import Iterable import numpy as np lowerCamelCase__ = typing.Union[Iterable[float], Iterable[int], np.ndarray] # noqa: UP007 lowerCamelCase__ = typing.Union[np.floataa, int, float] # noqa: UP007 def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: return np.sqrt(np.sum((np.asarray(__UpperCamelCase ) - np.asarray(__UpperCamelCase )) ** 2 ) ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: return sum((va - va) ** 2 for va, va in zip(__UpperCamelCase , __UpperCamelCase ) ) ** (1 / 2) if __name__ == "__main__": def lowerCAmelCase__ ( ) -> Optional[Any]: from timeit import timeit print('Without Numpy' ) print( timeit( 'euclidean_distance_no_np([1, 2, 3], [4, 5, 6])' , number=10_000 , globals=globals() , ) ) print('With Numpy' ) print( timeit( 'euclidean_distance([1, 2, 3], [4, 5, 6])' , number=10_000 , globals=globals() , ) ) benchmark()
369
from ..utils import DummyObject, requires_backends class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Union[str, Any] , *a : Optional[int] , **a : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : List[Any] , **a : int ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Any , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Optional[Any] , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Optional[int] , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : List[Any] , *a : List[str] , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Union[str, Any] , **a : Optional[int] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : List[Any] , *a : Dict , **a : List[str] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Dict , **a : List[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Dict , *a : str , **a : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Union[str, Any] , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : Union[str, Any] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Tuple , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] )
307
0
from typing import List, Optional import numpy as np from ...processing_utils import ProcessorMixin from ...utils import to_numpy class A__ ( _lowerCAmelCase ): lowercase = "EncodecFeatureExtractor" lowercase = ("T5Tokenizer", "T5TokenizerFast") def __init__( self : Tuple , a : str , a : Tuple ): '''simple docstring''' super().__init__(_lowercase , _lowercase ) lowerCAmelCase__ : List[str] = self.feature_extractor lowerCAmelCase__ : Optional[int] = False def _lowerCamelCase ( self : Union[str, Any] , a : int=None , a : Union[str, Any]=None , a : int=True ): '''simple docstring''' return self.tokenizer.get_decoder_prompt_ids(task=_lowercase , language=_lowercase , no_timestamps=_lowercase ) def __call__( self : Any , *a : Optional[int] , **a : int ): '''simple docstring''' if self._in_target_context_manager: return self.current_processor(*_lowercase , **_lowercase ) lowerCAmelCase__ : Any = kwargs.pop('audio' , _lowercase ) lowerCAmelCase__ : Optional[int] = kwargs.pop('sampling_rate' , _lowercase ) lowerCAmelCase__ : Any = kwargs.pop('text' , _lowercase ) if len(_lowercase ) > 0: lowerCAmelCase__ : List[str] = args[0] lowerCAmelCase__ : str = args[1:] if audio is None and text is None: raise ValueError('You need to specify either an `audio` or `text` input to process.' ) if text is not None: lowerCAmelCase__ : Dict = self.tokenizer(_lowercase , **_lowercase ) if audio is not None: lowerCAmelCase__ : Union[str, Any] = self.feature_extractor(_lowercase , *_lowercase , sampling_rate=_lowercase , **_lowercase ) if audio is None: return inputs elif text is None: return audio_inputs else: lowerCAmelCase__ : Dict = audio_inputs['input_values'] if "padding_mask" in audio_inputs: lowerCAmelCase__ : int = audio_inputs['padding_mask'] return inputs def _lowerCamelCase ( self : str , *a : Optional[int] , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = kwargs.pop('audio' , _lowercase ) lowerCAmelCase__ : str = kwargs.pop('padding_mask' , _lowercase ) if len(_lowercase ) > 0: lowerCAmelCase__ : str = args[0] lowerCAmelCase__ : Dict = args[1:] if audio_values is not None: return self._decode_audio(_lowercase , padding_mask=_lowercase ) else: return self.tokenizer.batch_decode(*_lowercase , **_lowercase ) def _lowerCamelCase ( self : Dict , *a : Dict , **a : Union[str, Any] ): '''simple docstring''' return self.tokenizer.decode(*_lowercase , **_lowercase ) def _lowerCamelCase ( self : Optional[int] , a : Union[str, Any] , a : Optional = None ): '''simple docstring''' lowerCAmelCase__ : str = to_numpy(_lowercase ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : int = audio_values.shape if padding_mask is None: return list(_lowercase ) lowerCAmelCase__ : str = to_numpy(_lowercase ) # match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding** # token (so that the generated audio values are **not** treated as padded tokens) lowerCAmelCase__ : str = seq_len - padding_mask.shape[-1] lowerCAmelCase__ : Dict = 1 - self.feature_extractor.padding_value lowerCAmelCase__ : List[Any] = np.pad(_lowercase , ((0, 0), (0, difference)) , 'constant' , constant_values=_lowercase ) lowerCAmelCase__ : Optional[Any] = audio_values.tolist() for i in range(_lowercase ): lowerCAmelCase__ : int = np.asarray(audio_values[i] )[ padding_mask[i][None, :] != self.feature_extractor.padding_value ] lowerCAmelCase__ : List[Any] = sliced_audio.reshape(_lowercase , -1 ) return audio_values
370
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class A__ : def __init__( self : List[str] , a : Any , a : Dict=13 , a : Optional[Any]=7 , a : Tuple=True , a : Tuple=True , a : Dict=False , a : Optional[Any]=True , a : Dict=99 , a : Tuple=32 , a : Optional[Any]=5 , a : str=4 , a : Union[str, Any]=37 , a : Any="gelu" , a : Dict=0.1 , a : Any=0.1 , a : Optional[int]=512 , a : Union[str, Any]=16 , a : Optional[int]=2 , a : Optional[Any]=0.0_2 , a : List[Any]=3 , a : Any=4 , a : Optional[int]=None , ): '''simple docstring''' lowerCAmelCase__ : List[str] = parent lowerCAmelCase__ : str = batch_size lowerCAmelCase__ : Optional[int] = seq_length lowerCAmelCase__ : Optional[Any] = is_training lowerCAmelCase__ : Tuple = use_input_mask lowerCAmelCase__ : List[Any] = use_token_type_ids lowerCAmelCase__ : str = use_labels lowerCAmelCase__ : Dict = vocab_size lowerCAmelCase__ : Union[str, Any] = hidden_size lowerCAmelCase__ : Optional[int] = num_hidden_layers lowerCAmelCase__ : List[Any] = num_attention_heads lowerCAmelCase__ : int = intermediate_size lowerCAmelCase__ : Union[str, Any] = hidden_act lowerCAmelCase__ : Union[str, Any] = hidden_dropout_prob lowerCAmelCase__ : Any = attention_probs_dropout_prob lowerCAmelCase__ : Dict = max_position_embeddings lowerCAmelCase__ : int = type_vocab_size lowerCAmelCase__ : int = type_sequence_label_size lowerCAmelCase__ : Dict = initializer_range lowerCAmelCase__ : List[str] = num_labels lowerCAmelCase__ : Any = num_choices lowerCAmelCase__ : List[Any] = scope def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase__ : Tuple = None if self.use_input_mask: lowerCAmelCase__ : str = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase__ : List[str] = None if self.use_token_type_ids: lowerCAmelCase__ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCAmelCase__ : Optional[Any] = None lowerCAmelCase__ : Dict = None lowerCAmelCase__ : str = None if self.use_labels: lowerCAmelCase__ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCAmelCase__ : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) lowerCAmelCase__ : List[str] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return LlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=a , initializer_range=self.initializer_range , ) def _lowerCamelCase ( self : Tuple , a : Dict , a : List[str] , a : str , a : Union[str, Any] , a : Optional[Any] , a : Dict , a : str ): '''simple docstring''' lowerCAmelCase__ : str = LlamaModel(config=a ) model.to(a ) model.eval() lowerCAmelCase__ : Dict = model(a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCamelCase ( self : int , a : Any , a : Union[str, Any] , a : Dict , a : Dict , a : List[Any] , a : Optional[Any] , a : int , a : Dict , a : Tuple , ): '''simple docstring''' lowerCAmelCase__ : int = True lowerCAmelCase__ : Dict = LlamaModel(a ) model.to(a ) model.eval() lowerCAmelCase__ : List[Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , ) lowerCAmelCase__ : Optional[int] = model( a , attention_mask=a , encoder_hidden_states=a , ) lowerCAmelCase__ : Union[str, Any] = model(a , attention_mask=a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCamelCase ( self : Union[str, Any] , a : int , a : List[Any] , a : int , a : Tuple , a : List[Any] , a : Union[str, Any] , a : Any , a : List[str] , a : List[str] , ): '''simple docstring''' lowerCAmelCase__ : List[Any] = LlamaForCausalLM(config=a ) model.to(a ) model.eval() lowerCAmelCase__ : Tuple = model(a , attention_mask=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowerCamelCase ( self : str , a : Any , a : Tuple , a : str , a : Union[str, Any] , a : Optional[Any] , a : List[Any] , a : Optional[Any] , a : Optional[Any] , a : List[str] , ): '''simple docstring''' lowerCAmelCase__ : str = True lowerCAmelCase__ : Optional[int] = True lowerCAmelCase__ : List[Any] = LlamaForCausalLM(config=a ) model.to(a ) model.eval() # first forward pass lowerCAmelCase__ : List[str] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , use_cache=a , ) lowerCAmelCase__ : List[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids lowerCAmelCase__ : Union[str, Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowerCAmelCase__ : Tuple = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and lowerCAmelCase__ : int = torch.cat([input_ids, next_tokens] , dim=-1 ) lowerCAmelCase__ : Any = torch.cat([input_mask, next_mask] , dim=-1 ) lowerCAmelCase__ : Union[str, Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , output_hidden_states=a , )['hidden_states'][0] lowerCAmelCase__ : Union[str, Any] = model( a , attention_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , past_key_values=a , output_hidden_states=a , )['hidden_states'][0] # select random slice lowerCAmelCase__ : Any = ids_tensor((1,) , output_from_past.shape[-1] ).item() lowerCAmelCase__ : Optional[int] = output_from_no_past[:, -3:, random_slice_idx].detach() lowerCAmelCase__ : Any = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(a , a , atol=1E-3 ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.prepare_config_and_inputs() ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : Any = config_and_inputs lowerCAmelCase__ : List[str] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class A__ ( __magic_name__ , __magic_name__ , __magic_name__ , unittest.TestCase ): lowercase = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () lowercase = (LlamaForCausalLM,) if is_torch_available() else () lowercase = ( { 'feature-extraction': LlamaModel, 'text-classification': LlamaForSequenceClassification, 'text-generation': LlamaForCausalLM, 'zero-shot': LlamaForSequenceClassification, } if is_torch_available() else {} ) lowercase = False lowercase = False def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Tuple = LlamaModelTester(self ) lowerCAmelCase__ : str = ConfigTester(self , config_class=a , hidden_size=37 ) def _lowerCamelCase ( self : str ): '''simple docstring''' self.config_tester.run_common_tests() def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCAmelCase__ : int = type self.model_tester.create_and_check_model(*a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : int = 3 lowerCAmelCase__ : Dict = input_dict['input_ids'] lowerCAmelCase__ : Optional[Any] = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Tuple = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : Tuple = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : str = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : List[Any] = 3 lowerCAmelCase__ : List[str] = 'single_label_classification' lowerCAmelCase__ : List[Any] = input_dict['input_ids'] lowerCAmelCase__ : List[Any] = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Optional[int] = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : int = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : Optional[int] = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Optional[Any] = 3 lowerCAmelCase__ : Optional[Any] = 'multi_label_classification' lowerCAmelCase__ : List[str] = input_dict['input_ids'] lowerCAmelCase__ : Tuple = input_ids.ne(1 ).to(a ) lowerCAmelCase__ : Any = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) lowerCAmelCase__ : Dict = LlamaForSequenceClassification(a ) model.to(a ) model.eval() lowerCAmelCase__ : Dict = model(a , attention_mask=a , labels=a ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' pass @parameterized.expand([('linear',), ('dynamic',)] ) def _lowerCamelCase ( self : Optional[int] , a : Dict ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Tuple = ids_tensor([1, 10] , config.vocab_size ) lowerCAmelCase__ : List[str] = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : List[Any] = LlamaModel(a ) original_model.to(a ) original_model.eval() lowerCAmelCase__ : List[Any] = original_model(a ).last_hidden_state lowerCAmelCase__ : str = original_model(a ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : Any = {'type': scaling_type, 'factor': 1_0.0} lowerCAmelCase__ : Union[str, Any] = LlamaModel(a ) scaled_model.to(a ) scaled_model.eval() lowerCAmelCase__ : Union[str, Any] = scaled_model(a ).last_hidden_state lowerCAmelCase__ : Optional[int] = scaled_model(a ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(a , a , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(a , a , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(a , a , atol=1E-5 ) ) @require_torch class A__ ( unittest.TestCase ): @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' , device_map='auto' ) lowerCAmelCase__ : Any = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 lowerCAmelCase__ : Dict = torch.tensor([[-6.6_5_5_0, -4.1_2_2_7, -4.9_8_5_9, -3.2_4_0_6, 0.8_2_6_2, -3.0_0_3_3, 1.2_9_6_4, -3.3_6_9_9]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : List[Any] = torch.tensor([-1_2.8_2_8_1, -7.4_4_5_3, -0.4_6_3_9, -8.0_6_2_5, -7.2_5_0_0, -8.0_0_0_0, -6.4_8_8_3, -7.7_6_9_5, -7.8_4_3_8, -7.0_3_1_2, -6.2_1_8_8, -7.1_3_2_8, -1.8_4_9_6, 1.9_9_6_1, -8.6_2_5_0, -6.7_2_2_7, -1_2.8_2_8_1, -6.9_4_9_2, -7.0_7_4_2, -7.7_8_5_2, -7.5_8_2_0, -7.9_0_6_2, -6.9_3_7_5, -7.9_8_0_5, -8.3_4_3_8, -8.1_5_6_2, -8.0_4_6_9, -7.6_2_5_0, -7.7_4_2_2, -7.3_3_9_8,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : Optional[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' , device_map='auto' ) lowerCAmelCase__ : List[str] = model(torch.tensor(a ) ) # Expected mean on dim = -1 lowerCAmelCase__ : Union[str, Any] = torch.tensor([[-2.0_6_2_2, -1.2_7_9_4, -1.1_6_3_8, -0.9_7_8_8, -1.4_6_0_3, -1.0_2_3_8, -1.7_8_9_3, -1.4_4_1_1]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : Any = torch.tensor([-8.1_4_0_6, -8.0_5_4_7, 2.7_4_6_1, -1.2_3_4_4, -0.1_4_4_8, -1.8_2_6_2, -1.0_0_2_0, -1.8_1_5_4, -1.6_8_9_5, -1.8_5_1_6, -2.3_5_7_4, -0.9_2_7_7, 3.7_5_9_8, 6.5_7_4_2, -1.2_9_9_8, -0.1_1_7_7, -8.1_4_0_6, -2.9_6_8_8, -2.9_1_9_9, -3.1_6_9_9, -3.5_2_5_4, -2.3_5_5_5, -2.7_9_8_8, -3.4_1_4_1, -2.8_2_6_2, -4.5_1_9_5, -3.3_3_7_9, -3.3_1_6_4, -2.7_8_3_2, -3.0_2_7_3] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : int = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : Optional[int] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' , device_map='auto' ) lowerCAmelCase__ : str = model(torch.tensor(a ) ) # Expected mean on dim = -1 lowerCAmelCase__ : str = torch.tensor([[-0.8_5_6_2, -1.8_5_2_0, -0.7_5_5_1, -0.4_1_6_2, -1.5_1_6_1, -1.2_0_3_8, -2.4_8_2_3, -2.3_2_5_4]] ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCAmelCase__ : List[str] = torch.tensor([-2.2_2_2_7, 4.8_8_2_8, 0.9_0_2_3, -0.4_5_7_8, -0.7_8_7_1, -0.1_0_3_3, -0.6_2_2_1, -0.5_7_8_6, -0.7_8_0_3, -1.0_6_7_4, -1.2_9_2_0, -0.1_5_7_0, 0.8_0_0_8, 2.0_7_2_3, -0.9_4_9_7, 0.2_7_7_1, -2.2_2_2_7, -0.7_6_1_2, -1.4_3_4_6, -1.2_0_6_1, -1.6_4_2_6, -0.3_0_0_0, -0.7_1_3_9, -1.1_9_3_4, -1.8_6_9_1, -1.6_9_7_3, -1.5_9_4_7, -1.2_7_0_5, -0.3_5_2_3, -0.5_5_1_3] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = [1, 306, 4_658, 278, 6_593, 310, 2_834, 338] lowerCAmelCase__ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' , device_map='auto' ) lowerCAmelCase__ : List[str] = model(torch.tensor(a ) ) lowerCAmelCase__ : int = torch.tensor( [[-4.2_3_2_7, -3.3_3_6_0, -4.6_6_6_5, -4.7_6_3_1, -1.8_1_8_0, -3.4_1_7_0, -1.4_2_1_1, -3.1_8_1_0]] , dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) , a , atol=1E-2 , rtol=1E-2 ) # fmt: off lowerCAmelCase__ : Optional[int] = torch.tensor([-9.4_9_2_2, -3.9_5_5_1, 1.7_9_9_8, -5.6_7_5_8, -5.1_0_5_5, -5.8_9_8_4, -4.8_3_2_0, -6.8_0_8_6, -6.5_3_9_1, -5.6_1_7_2, -5.5_8_2_0, -5.5_3_5_2, 1.7_8_8_1, 3.6_2_8_9, -6.5_1_1_7, -3.4_7_8_5, -9.5_0_0_0, -6.0_3_5_2, -6.8_1_2_5, -6.0_1_9_5, -6.6_8_3_6, -5.4_7_2_7, -6.2_8_1_2, -6.0_3_9_1, -7.3_3_9_8, -7.4_2_9_7, -7.4_8_4_4, -6.5_8_2_0, -5.8_7_8_9, -5.5_3_1_2] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , a , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' lowerCAmelCase__ : Tuple = 'Simply put, the theory of relativity states that ' lowerCAmelCase__ : Dict = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) lowerCAmelCase__ : Dict = tokenizer.encode(a , return_tensors='pt' ) lowerCAmelCase__ : str = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' , device_map='sequential' , use_safetensors=a ) # greedy generation outputs lowerCAmelCase__ : Optional[Any] = model.generate(a , max_new_tokens=64 , top_p=a , temperature=1 , do_sample=a ) lowerCAmelCase__ : Tuple = tokenizer.decode(generated_ids[0] , skip_special_tokens=a ) self.assertEqual(a , a )
307
0
import operator def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = False , SCREAMING_SNAKE_CASE_ = None ) -> Dict: lowerCAmelCase__ : int = operator.lt if reverse else operator.gt lowerCAmelCase__ : Any = solution or [] if not arr: return solution lowerCAmelCase__ : int = [arr.pop(0 )] for i, item in enumerate(a__ ): if _operator(a__ , sublist[-1] ): sublist.append(a__ ) arr.pop(a__ ) # merging sublist into solution list if not solution: solution.extend(a__ ) else: while sublist: lowerCAmelCase__ : str = sublist.pop(0 ) for i, xx in enumerate(a__ ): if not _operator(a__ , a__ ): solution.insert(a__ , a__ ) break else: solution.append(a__ ) strand_sort(a__ , a__ , a__ ) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
371
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """microsoft/unispeech-large-1500h-cv""": ( """https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json""" ), # See all UniSpeech models at https://huggingface.co/models?filter=unispeech } class A__ ( __magic_name__ ): lowercase = 'unispeech' def __init__( self : Any , a : List[Any]=32 , a : List[Any]=768 , a : Any=12 , a : List[str]=12 , a : List[Any]=3_072 , a : Any="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : List[str]=0.1 , a : Union[str, Any]=0.0 , a : str=0.0 , a : int=0.1 , a : List[str]=0.1 , a : List[Any]=0.0_2 , a : Optional[int]=1E-5 , a : Optional[int]="group" , a : Optional[Any]="gelu" , a : List[Any]=(512, 512, 512, 512, 512, 512, 512) , a : Optional[Any]=(5, 2, 2, 2, 2, 2, 2) , a : List[str]=(10, 3, 3, 3, 3, 2, 2) , a : Union[str, Any]=False , a : Union[str, Any]=128 , a : Tuple=16 , a : Dict=False , a : str=True , a : str=0.0_5 , a : Union[str, Any]=10 , a : Tuple=2 , a : int=0.0 , a : Optional[Any]=10 , a : List[str]=0 , a : str=320 , a : List[str]=2 , a : Optional[Any]=0.1 , a : Any=100 , a : Dict=256 , a : Any=256 , a : Dict=0.1 , a : List[Any]="mean" , a : Dict=False , a : str=False , a : Optional[int]=256 , a : Any=80 , a : List[Any]=0 , a : Optional[int]=1 , a : int=2 , a : List[Any]=0.5 , **a : int , ): '''simple docstring''' super().__init__(**a , pad_token_id=a , bos_token_id=a , eos_token_id=a ) lowerCAmelCase__ : List[str] = hidden_size lowerCAmelCase__ : List[str] = feat_extract_norm lowerCAmelCase__ : Optional[Any] = feat_extract_activation lowerCAmelCase__ : str = list(a ) lowerCAmelCase__ : List[str] = list(a ) lowerCAmelCase__ : Tuple = list(a ) lowerCAmelCase__ : Dict = conv_bias lowerCAmelCase__ : Optional[int] = num_conv_pos_embeddings lowerCAmelCase__ : Any = num_conv_pos_embedding_groups lowerCAmelCase__ : str = len(self.conv_dim ) lowerCAmelCase__ : Any = num_hidden_layers lowerCAmelCase__ : Dict = intermediate_size lowerCAmelCase__ : Dict = hidden_act lowerCAmelCase__ : Union[str, Any] = num_attention_heads lowerCAmelCase__ : Union[str, Any] = hidden_dropout lowerCAmelCase__ : Tuple = attention_dropout lowerCAmelCase__ : str = activation_dropout lowerCAmelCase__ : Any = feat_proj_dropout lowerCAmelCase__ : List[Any] = final_dropout lowerCAmelCase__ : Tuple = layerdrop lowerCAmelCase__ : Any = layer_norm_eps lowerCAmelCase__ : Dict = initializer_range lowerCAmelCase__ : Optional[Any] = num_ctc_classes lowerCAmelCase__ : Tuple = vocab_size lowerCAmelCase__ : Dict = do_stable_layer_norm lowerCAmelCase__ : List[Any] = use_weighted_layer_sum lowerCAmelCase__ : Any = classifier_proj_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)`, but is `len(config.conv_dim) =' f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCAmelCase__ : Union[str, Any] = apply_spec_augment lowerCAmelCase__ : Any = mask_time_prob lowerCAmelCase__ : Dict = mask_time_length lowerCAmelCase__ : Tuple = mask_time_min_masks lowerCAmelCase__ : Optional[int] = mask_feature_prob lowerCAmelCase__ : Optional[Any] = mask_feature_length lowerCAmelCase__ : int = mask_feature_min_masks # parameters for pretraining with codevector quantized representations lowerCAmelCase__ : int = num_codevectors_per_group lowerCAmelCase__ : Any = num_codevector_groups lowerCAmelCase__ : Any = contrastive_logits_temperature lowerCAmelCase__ : int = feat_quantizer_dropout lowerCAmelCase__ : List[Any] = num_negatives lowerCAmelCase__ : List[str] = codevector_dim lowerCAmelCase__ : Optional[int] = proj_codevector_dim lowerCAmelCase__ : Dict = diversity_loss_weight # ctc loss lowerCAmelCase__ : Any = ctc_loss_reduction lowerCAmelCase__ : Any = ctc_zero_infinity # pretraining loss lowerCAmelCase__ : Union[str, Any] = replace_prob @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
307
0
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = """▁""" lowerCamelCase__ = {"""vocab_file""": """sentencepiece.bpe.model"""} lowerCamelCase__ = { """vocab_file""": { """facebook/mbart-large-en-ro""": ( """https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model""" ), """facebook/mbart-large-cc25""": ( """https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model""" ), } } lowerCamelCase__ = { """facebook/mbart-large-en-ro""": 1024, """facebook/mbart-large-cc25""": 1024, } # fmt: off lowerCamelCase__ = ["""ar_AR""", """cs_CZ""", """de_DE""", """en_XX""", """es_XX""", """et_EE""", """fi_FI""", """fr_XX""", """gu_IN""", """hi_IN""", """it_IT""", """ja_XX""", """kk_KZ""", """ko_KR""", """lt_LT""", """lv_LV""", """my_MM""", """ne_NP""", """nl_XX""", """ro_RO""", """ru_RU""", """si_LK""", """tr_TR""", """vi_VN""", """zh_CN"""] class A__ ( snake_case_ ): lowercase = VOCAB_FILES_NAMES lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase = PRETRAINED_VOCAB_FILES_MAP lowercase = ["input_ids", "attention_mask"] lowercase = [] lowercase = [] def __init__( self : Dict , a : Optional[int] , a : List[str]="<s>" , a : Optional[int]="</s>" , a : List[str]="</s>" , a : Any="<s>" , a : Optional[int]="<unk>" , a : Union[str, Any]="<pad>" , a : Any="<mask>" , a : str=None , a : Union[str, Any]=None , a : str=None , a : Optional[Dict[str, Any]] = None , a : Dict=None , **a : Union[str, Any] , ): '''simple docstring''' lowerCAmelCase__ : Tuple = AddedToken(_A , lstrip=_A , rstrip=_A ) if isinstance(_A , _A ) else mask_token lowerCAmelCase__ : Any = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_A , eos_token=_A , unk_token=_A , sep_token=_A , cls_token=_A , pad_token=_A , mask_token=_A , tokenizer_file=_A , src_lang=_A , tgt_lang=_A , additional_special_tokens=_A , sp_model_kwargs=self.sp_model_kwargs , **_A , ) lowerCAmelCase__ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_A ) ) lowerCAmelCase__ : int = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # Mimic fairseq token-to-id alignment for the first 4 token lowerCAmelCase__ : Any = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab lowerCAmelCase__ : Optional[int] = 1 lowerCAmelCase__ : Optional[Any] = len(self.sp_model ) lowerCAmelCase__ : str = { code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(_A ) } lowerCAmelCase__ : List[Any] = {v: k for k, v in self.lang_code_to_id.items()} lowerCAmelCase__ : str = len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset self.fairseq_tokens_to_ids.update(self.lang_code_to_id ) lowerCAmelCase__ : int = {v: k for k, v in self.fairseq_tokens_to_ids.items()} lowerCAmelCase__ : List[Any] = list(self.lang_code_to_id.keys() ) if additional_special_tokens is not None: # Only add those special tokens if they are not already there. self._additional_special_tokens.extend( [t for t in additional_special_tokens if t not in self._additional_special_tokens] ) lowerCAmelCase__ : List[str] = src_lang if src_lang is not None else 'en_XX' lowerCAmelCase__ : Optional[Any] = self.lang_code_to_id[self._src_lang] lowerCAmelCase__ : Optional[Any] = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) def __getstate__( self : int ): '''simple docstring''' lowerCAmelCase__ : Dict = self.__dict__.copy() lowerCAmelCase__ : List[Any] = None lowerCAmelCase__ : List[Any] = self.sp_model.serialized_model_proto() return state def __setstate__( self : Optional[Any] , a : str ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): lowerCAmelCase__ : List[Any] = {} lowerCAmelCase__ : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) @property def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' return len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset + 1 # Plus 1 for the mask token @property def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return self._src_lang @src_lang.setter def _lowerCamelCase ( self : Tuple , a : str ): '''simple docstring''' lowerCAmelCase__ : Any = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _lowerCamelCase ( self : Dict , a : List[int] , a : Optional[List[int]] = None , a : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_A , token_ids_a=_A , already_has_special_tokens=_A ) lowerCAmelCase__ : Union[str, Any] = [1] * len(self.prefix_tokens ) lowerCAmelCase__ : Optional[Any] = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(_A )) + suffix_ones return prefix_ones + ([0] * len(_A )) + ([0] * len(_A )) + suffix_ones def _lowerCamelCase ( self : List[Any] , a : List[int] , a : Optional[List[int]] = None ): '''simple docstring''' if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _lowerCamelCase ( self : List[Any] , a : List[int] , a : Optional[List[int]] = None ): '''simple docstring''' lowerCAmelCase__ : Tuple = [self.sep_token_id] lowerCAmelCase__ : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _lowerCamelCase ( self : List[str] , a : Dict , a : str , a : Optional[str] , a : Optional[str] , **a : Optional[int] ): '''simple docstring''' if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCAmelCase__ : List[str] = src_lang lowerCAmelCase__ : Optional[Any] = self(_A , add_special_tokens=_A , return_tensors=_A , **_A ) lowerCAmelCase__ : Tuple = self.convert_tokens_to_ids(_A ) lowerCAmelCase__ : Optional[Any] = tgt_lang_id return inputs def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[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 : Optional[int] , a : str ): '''simple docstring''' return self.sp_model.encode(_A , out_type=_A ) def _lowerCamelCase ( self : Optional[Any] , a : List[str] ): '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] lowerCAmelCase__ : Dict = self.sp_model.PieceToId(_A ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def _lowerCamelCase ( self : Union[str, Any] , a : List[str] ): '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def _lowerCamelCase ( self : List[str] , a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = ''.join(_A ).replace(_A , ' ' ).strip() return out_string def _lowerCamelCase ( self : Tuple , a : str , a : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(_A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return lowerCAmelCase__ : List[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: lowerCAmelCase__ : List[str] = self.sp_model.serialized_model_proto() fi.write(_A ) return (out_vocab_file,) def _lowerCamelCase ( self : Optional[int] , a : List[str] , a : str = "en_XX" , a : Optional[List[str]] = None , a : str = "ro_RO" , **a : str , ): '''simple docstring''' lowerCAmelCase__ : int = src_lang lowerCAmelCase__ : Any = tgt_lang return super().prepare_seqaseq_batch(_A , _A , **_A ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return self.set_src_lang_special_tokens(self.src_lang ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _lowerCamelCase ( self : List[str] , a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = self.lang_code_to_id[src_lang] lowerCAmelCase__ : List[str] = [] lowerCAmelCase__ : int = [self.eos_token_id, self.cur_lang_code] def _lowerCamelCase ( self : str , a : str ): '''simple docstring''' lowerCAmelCase__ : int = self.lang_code_to_id[lang] lowerCAmelCase__ : Tuple = [] lowerCAmelCase__ : Union[str, Any] = [self.eos_token_id, self.cur_lang_code]
350
import torch from torch import nn class A__ ( nn.Module ): def __init__( self : Optional[int] , a : Union[str, Any] , a : str , a : str , a : List[Any] , a : List[Any]=1 , a : Tuple=False ): '''simple docstring''' super().__init__() lowerCAmelCase__ : Dict = n_token lowerCAmelCase__ : Any = d_embed lowerCAmelCase__ : str = d_proj lowerCAmelCase__ : int = cutoffs + [n_token] lowerCAmelCase__ : Union[str, Any] = [0] + self.cutoffs lowerCAmelCase__ : str = div_val lowerCAmelCase__ : Tuple = self.cutoffs[0] lowerCAmelCase__ : Dict = len(self.cutoffs ) - 1 lowerCAmelCase__ : Any = self.shortlist_size + self.n_clusters if self.n_clusters > 0: lowerCAmelCase__ : int = nn.Parameter(torch.zeros(self.n_clusters , self.d_embed ) ) lowerCAmelCase__ : Optional[Any] = nn.Parameter(torch.zeros(self.n_clusters ) ) lowerCAmelCase__ : Optional[int] = nn.ModuleList() lowerCAmelCase__ : Tuple = nn.ParameterList() if div_val == 1: for i in range(len(self.cutoffs ) ): if d_proj != d_embed: self.out_projs.append(nn.Parameter(torch.FloatTensor(a , a ) ) ) else: self.out_projs.append(a ) self.out_layers.append(nn.Linear(a , a ) ) else: for i in range(len(self.cutoffs ) ): lowerCAmelCase__ , lowerCAmelCase__ : Any = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : Optional[Any] = d_embed // (div_val**i) self.out_projs.append(nn.Parameter(torch.FloatTensor(a , a ) ) ) self.out_layers.append(nn.Linear(a , r_idx - l_idx ) ) lowerCAmelCase__ : Tuple = keep_order def _lowerCamelCase ( self : Optional[int] , a : List[str] , a : int , a : List[str] , a : str ): '''simple docstring''' if proj is None: lowerCAmelCase__ : Tuple = nn.functional.linear(a , a , bias=a ) else: # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1: lowerCAmelCase__ : int = nn.functional.linear(a , proj.t().contiguous() ) lowerCAmelCase__ : Tuple = nn.functional.linear(a , a , bias=a ) # else: # logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t())) # if bias is not None: # logit = logit + bias return logit def _lowerCamelCase ( self : List[str] , a : List[Any] , a : Optional[int]=None , a : Tuple=False ): '''simple docstring''' if labels is not None: # Shift so that tokens < n predict n lowerCAmelCase__ : str = hidden[..., :-1, :].contiguous() lowerCAmelCase__ : Optional[Any] = labels[..., 1:].contiguous() lowerCAmelCase__ : List[Any] = hidden.view(-1 , hidden.size(-1 ) ) lowerCAmelCase__ : Tuple = labels.view(-1 ) if hidden.size(0 ) != labels.size(0 ): raise RuntimeError('Input and labels should have the same size in the batch dimension.' ) else: lowerCAmelCase__ : Optional[Any] = hidden.view(-1 , hidden.size(-1 ) ) if self.n_clusters == 0: lowerCAmelCase__ : Optional[Any] = self._compute_logit(a , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) if labels is not None: lowerCAmelCase__ : str = labels != -100 lowerCAmelCase__ : int = torch.zeros_like(a , dtype=hidden.dtype , device=hidden.device ) lowerCAmelCase__ : List[str] = ( -nn.functional.log_softmax(a , dim=-1 )[mask].gather(1 , labels[mask].unsqueeze(1 ) ).squeeze(1 ) ) else: lowerCAmelCase__ : Optional[Any] = nn.functional.log_softmax(a , dim=-1 ) else: # construct weights and biases lowerCAmelCase__ , lowerCAmelCase__ : int = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : Any = self.out_layers[0].weight[l_idx:r_idx] lowerCAmelCase__ : Any = self.out_layers[0].bias[l_idx:r_idx] else: lowerCAmelCase__ : Optional[Any] = self.out_layers[i].weight lowerCAmelCase__ : Optional[int] = self.out_layers[i].bias if i == 0: lowerCAmelCase__ : Dict = torch.cat([weight_i, self.cluster_weight] , dim=0 ) lowerCAmelCase__ : Union[str, Any] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(a ) biases.append(a ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = weights[0], biases[0], self.out_projs[0] lowerCAmelCase__ : List[Any] = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : Union[str, Any] = nn.functional.log_softmax(a , dim=1 ) if labels is None: lowerCAmelCase__ : Tuple = hidden.new_empty((head_logit.size(0 ), self.n_token) ) else: lowerCAmelCase__ : Dict = torch.zeros_like(a , dtype=hidden.dtype , device=hidden.device ) lowerCAmelCase__ : Tuple = 0 lowerCAmelCase__ : Union[str, Any] = [0] + self.cutoffs for i in range(len(a ) - 1 ): lowerCAmelCase__ , lowerCAmelCase__ : Tuple = cutoff_values[i], cutoff_values[i + 1] if labels is not None: lowerCAmelCase__ : Tuple = (labels >= l_idx) & (labels < r_idx) lowerCAmelCase__ : int = mask_i.nonzero().squeeze() if indices_i.numel() == 0: continue lowerCAmelCase__ : Tuple = labels.index_select(0 , a ) - l_idx lowerCAmelCase__ : Any = head_logprob.index_select(0 , a ) lowerCAmelCase__ : Optional[int] = hidden.index_select(0 , a ) else: lowerCAmelCase__ : Any = hidden if i == 0: if labels is not None: lowerCAmelCase__ : Union[str, Any] = head_logprob_i.gather(1 , target_i[:, None] ).squeeze(1 ) else: lowerCAmelCase__ : List[str] = head_logprob[:, : self.cutoffs[0]] else: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Any = weights[i], biases[i], self.out_projs[i] lowerCAmelCase__ : Union[str, Any] = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : Optional[int] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : List[Any] = self.cutoffs[0] + i - 1 # No probability for the head cluster if labels is not None: lowerCAmelCase__ : List[str] = head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather( 1 , target_i[:, None] ).squeeze(1 ) else: lowerCAmelCase__ : Tuple = head_logprob[:, cluster_prob_idx, None] + tail_logprob_i lowerCAmelCase__ : Union[str, Any] = logprob_i if labels is not None: if (hasattr(self , 'keep_order' ) and self.keep_order) or keep_order: out.index_copy_(0 , a , -logprob_i ) else: out[offset : offset + logprob_i.size(0 )].copy_(-logprob_i ) offset += logprob_i.size(0 ) return out def _lowerCamelCase ( self : List[Any] , a : Any ): '''simple docstring''' if self.n_clusters == 0: lowerCAmelCase__ : Union[str, Any] = self._compute_logit(a , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) return nn.functional.log_softmax(a , dim=-1 ) else: # construct weights and biases lowerCAmelCase__ , lowerCAmelCase__ : str = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.cutoff_ends[i], self.cutoff_ends[i + 1] lowerCAmelCase__ : str = self.out_layers[0].weight[l_idx:r_idx] lowerCAmelCase__ : Dict = self.out_layers[0].bias[l_idx:r_idx] else: lowerCAmelCase__ : int = self.out_layers[i].weight lowerCAmelCase__ : int = self.out_layers[i].bias if i == 0: lowerCAmelCase__ : Optional[int] = torch.cat([weight_i, self.cluster_weight] , dim=0 ) lowerCAmelCase__ : Union[str, Any] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(a ) biases.append(a ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : str = weights[0], biases[0], self.out_projs[0] lowerCAmelCase__ : Dict = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : List[Any] = hidden.new_empty((head_logit.size(0 ), self.n_token) ) lowerCAmelCase__ : Optional[Any] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : List[Any] = [0] + self.cutoffs for i in range(len(a ) - 1 ): lowerCAmelCase__ , lowerCAmelCase__ : str = cutoff_values[i], cutoff_values[i + 1] if i == 0: lowerCAmelCase__ : Union[str, Any] = head_logprob[:, : self.cutoffs[0]] else: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = weights[i], biases[i], self.out_projs[i] lowerCAmelCase__ : Dict = self._compute_logit(a , a , a , a ) lowerCAmelCase__ : List[str] = nn.functional.log_softmax(a , dim=1 ) lowerCAmelCase__ : Dict = head_logprob[:, -i] + tail_logprob_i lowerCAmelCase__ : List[str] = logprob_i return out
307
0
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 lowerCamelCase__ = "true" def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=82 , SCREAMING_SNAKE_CASE_=16 ) -> List[Any]: set_seed(42 ) lowerCAmelCase__ : Dict = RegressionModel() lowerCAmelCase__ : int = deepcopy(A__ ) lowerCAmelCase__ : List[Any] = RegressionDataset(length=A__ ) lowerCAmelCase__ : Any = DataLoader(A__ , batch_size=A__ ) model.to(accelerator.device ) lowerCAmelCase__ : Tuple = accelerator.prepare(A__ , A__ ) return model, ddp_model, dataloader def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False ) -> Dict: lowerCAmelCase__ : Dict = AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased' ) lowerCAmelCase__ : List[Any] = load_dataset('glue' , 'mrpc' , split='validation' ) def tokenize_function(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Dict = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=A__ , max_length=A__ ) return outputs with accelerator.main_process_first(): lowerCAmelCase__ : Tuple = dataset.map( A__ , batched=A__ , remove_columns=['idx', 'sentence1', 'sentence2'] , ) lowerCAmelCase__ : Any = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(SCREAMING_SNAKE_CASE_ ): if use_longest: return tokenizer.pad(A__ , padding='longest' , return_tensors='pt' ) return tokenizer.pad(A__ , padding='max_length' , max_length=128 , return_tensors='pt' ) return DataLoader(A__ , shuffle=A__ , collate_fn=A__ , batch_size=16 ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: lowerCAmelCase__ : Optional[int] = Accelerator(dispatch_batches=A__ , split_batches=A__ ) lowerCAmelCase__ : int = get_dataloader(A__ , not dispatch_batches ) lowerCAmelCase__ : int = AutoModelForSequenceClassification.from_pretrained( 'hf-internal-testing/mrpc-bert-base-cased' , return_dict=A__ ) lowerCAmelCase__ : Optional[Any] = accelerator.prepare(A__ , A__ ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: lowerCAmelCase__ : List[str] = [] for batch in dataloader: lowerCAmelCase__ : Optional[Any] = batch.values() with torch.no_grad(): lowerCAmelCase__ : int = model(A__ ) lowerCAmelCase__ : Any = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) lowerCAmelCase__ : List[Any] = [], [] for logit, targ in logits_and_targets: logits.append(A__ ) targs.append(A__ ) lowerCAmelCase__ : List[Any] = torch.cat(A__ ), torch.cat(A__ ) return logits, targs def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=82 , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=16 ) -> Union[str, Any]: lowerCAmelCase__ : Any = get_basic_setup(A__ , A__ , A__ ) lowerCAmelCase__ : List[Any] = generate_predictions(A__ , A__ , A__ ) assert ( len(A__ ) == num_samples ), F'''Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(A__ )}''' def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = False , SCREAMING_SNAKE_CASE_ = False ) -> str: lowerCAmelCase__ : Tuple = evaluate.load('glue' , 'mrpc' ) lowerCAmelCase__ : Tuple = get_mrpc_setup(A__ , A__ ) # First do baseline lowerCAmelCase__ : Tuple = setup["""no"""] model.to(A__ ) model.eval() for batch in dataloader: batch.to(A__ ) with torch.inference_mode(): lowerCAmelCase__ : Dict = model(**A__ ) lowerCAmelCase__ : Any = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=A__ , references=batch['labels'] ) lowerCAmelCase__ : Any = metric.compute() # Then do distributed lowerCAmelCase__ : str = setup["""ddp"""] model.eval() for batch in dataloader: with torch.inference_mode(): lowerCAmelCase__ : Optional[Any] = model(**A__ ) lowerCAmelCase__ : Any = outputs.logits.argmax(dim=-1 ) lowerCAmelCase__ : List[str] = batch["""labels"""] lowerCAmelCase__ : Optional[int] = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=A__ , references=A__ ) lowerCAmelCase__ : Optional[int] = 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 lowerCAmelCase__ ( ) -> Optional[Any]: lowerCAmelCase__ : Dict = Accelerator(split_batches=A__ , dispatch_batches=A__ ) 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(A__ , A__ ) 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]: lowerCAmelCase__ : Tuple = Accelerator(split_batches=A__ , dispatch_batches=A__ ) if accelerator.is_local_main_process: print(F'''With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99''' ) test_torch_metrics(A__ , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test last batch is not dropped when perfectly divisible**' ) lowerCAmelCase__ : Any = Accelerator() test_torch_metrics(A__ , 512 ) accelerator.state._reset_state() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> int: main() if __name__ == "__main__": main()
351
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate lowerCamelCase__ = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow("""""", """|""", """|"""), datarow=DataRow("""""", """|""", """|"""), padding=1, with_header_hide=None, ) lowerCamelCase__ = [] lowerCamelCase__ = [] lowerCamelCase__ = {"""type""": """section""", """text""": {"""type""": """plain_text""", """text""": """No failed tests! 🤗""", """emoji""": True}} lowerCamelCase__ = [ { """type""": """header""", """text""": { """type""": """plain_text""", """text""": F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", """emoji""": True, }, } ] lowerCamelCase__ = 0 for log in Path().glob("""*.log"""): lowerCamelCase__ = 0 with open(log, """r""") as f: for line in f: lowerCamelCase__ = json.loads(line) if line.get("""nodeid""", """""") != "": lowerCamelCase__ = line["""nodeid"""] if line.get("""duration""", None) is not None: lowerCamelCase__ = F"""{line["duration"]:.4f}""" if line.get("""outcome""", """""") == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split("""_""")[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) lowerCamelCase__ = [] log.unlink() lowerCamelCase__ = """""" lowerCamelCase__ = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" lowerCamelCase__ = [] lowerCamelCase__ = {} for test in failed_tests: lowerCamelCase__ = test[0].split("""::""") lowerCamelCase__ = data[0].split("""/""")[-1] if data[0] not in filesafailed: lowerCamelCase__ = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) lowerCamelCase__ = [test[0] for test in failed_table] lowerCamelCase__ = list(set(files)) # Count number of instances in failed_tests lowerCamelCase__ = [] for file in individual_files: table.append([file, len(filesafailed[file])]) lowerCamelCase__ = tabulate( table, headers=["""Test Location""", """Num Failed"""], tablefmt=hf_table_format, stralign="""right""", ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 3000: lowerCamelCase__ = """Too many failed tests, please see the full report in the Action results.""" lowerCamelCase__ = len(err) + 10 lowerCamelCase__ = message[: 3000 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: lowerCamelCase__ = """No failed tests! 🤗""" print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get("""TEST_TYPE""", """""") != "": from slack_sdk import WebClient lowerCamelCase__ = WebClient(token=os.environ["""SLACK_API_TOKEN"""]) if message != "No failed tests! 🤗": lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": message, }, } payload.append(md_report) lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": """*For more details:*""", }, """accessory""": { """type""": """button""", """text""": { """type""": """plain_text""", """text""": """Check Action results""", """emoji""": True, }, """url""": F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) lowerCamelCase__ = { """type""": """context""", """elements""": [ { """type""": """plain_text""", """text""": F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) lowerCamelCase__ = client.chat_postMessage(channel="""#accelerate-ci-daily""", text=message, blocks=payload) lowerCamelCase__ = response.data["""ts"""] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name lowerCamelCase__ = """""" for i, row in enumerate(test_failures): if row[0] != test_class: lowerCamelCase__ = row[0] else: lowerCamelCase__ = """""" lowerCamelCase__ = { """type""": """section""", """text""": { """type""": """mrkdwn""", """text""": F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel="""#accelerate-ci-daily""", thread_ts=ts, blocks=[payload], )
307
0
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCamelCase__ = { """configuration_efficientnet""": [ """EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """EfficientNetConfig""", """EfficientNetOnnxConfig""", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""EfficientNetImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST""", """EfficientNetForImageClassification""", """EfficientNetModel""", """EfficientNetPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
352
import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs lowerCamelCase__ = imread(r"""digital_image_processing/image_data/lena_small.jpg""") lowerCamelCase__ = cvtColor(img, COLOR_BGR2GRAY) def lowerCAmelCase__ ( ) -> Dict: lowerCAmelCase__ : List[Any] = cn.convert_to_negative(SCREAMING_SNAKE_CASE_ ) # assert negative_img array for at least one True assert negative_img.any() def lowerCAmelCase__ ( ) -> Optional[Any]: with Image.open('digital_image_processing/image_data/lena_small.jpg' ) as img: # Work around assertion for response assert str(cc.change_contrast(SCREAMING_SNAKE_CASE_ , 110 ) ).startswith( '<PIL.Image.Image image mode=RGB size=100x100 at' ) def lowerCAmelCase__ ( ) -> Tuple: lowerCAmelCase__ : str = canny.gen_gaussian_kernel(9 , sigma=1.4 ) # Assert ambiguous array assert resp.all() def lowerCAmelCase__ ( ) -> Tuple: lowerCAmelCase__ : Tuple = imread('digital_image_processing/image_data/lena_small.jpg' , 0 ) # assert ambiguous array for all == True assert canny_img.all() lowerCAmelCase__ : Optional[Any] = canny.canny(SCREAMING_SNAKE_CASE_ ) # assert canny array for at least one True assert canny_array.any() def lowerCAmelCase__ ( ) -> Optional[int]: assert gg.gaussian_filter(SCREAMING_SNAKE_CASE_ , 5 , sigma=0.9 ).all() def lowerCAmelCase__ ( ) -> Dict: # laplace diagonals lowerCAmelCase__ : Union[str, Any] = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]] ) lowerCAmelCase__ : int = conv.img_convolve(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).astype(SCREAMING_SNAKE_CASE_ ) assert res.any() def lowerCAmelCase__ ( ) -> List[str]: assert med.median_filter(SCREAMING_SNAKE_CASE_ , 3 ).any() def lowerCAmelCase__ ( ) -> Any: lowerCAmelCase__ , lowerCAmelCase__ : str = sob.sobel_filter(SCREAMING_SNAKE_CASE_ ) assert grad.any() and theta.any() def lowerCAmelCase__ ( ) -> Any: lowerCAmelCase__ : int = sp.make_sepia(SCREAMING_SNAKE_CASE_ , 20 ) assert sepia.all() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = "digital_image_processing/image_data/lena_small.jpg" ) -> Optional[Any]: lowerCAmelCase__ : List[Any] = bs.Burkes(imread(SCREAMING_SNAKE_CASE_ , 1 ) , 120 ) burkes.process() assert burkes.output_img.any() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = "digital_image_processing/image_data/lena_small.jpg" , ) -> Any: lowerCAmelCase__ : Dict = rs.NearestNeighbour(imread(SCREAMING_SNAKE_CASE_ , 1 ) , 400 , 200 ) nn.process() assert nn.output.any() def lowerCAmelCase__ ( ) -> int: lowerCAmelCase__ : int = 'digital_image_processing/image_data/lena.jpg' # Reading the image and converting it to grayscale. lowerCAmelCase__ : List[str] = imread(SCREAMING_SNAKE_CASE_ , 0 ) # Test for get_neighbors_pixel function() return not None lowerCAmelCase__ : str = 0 lowerCAmelCase__ : str = 0 lowerCAmelCase__ : List[str] = image[x_coordinate][y_coordinate] lowerCAmelCase__ : Dict = lbp.get_neighbors_pixel( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image lowerCAmelCase__ : List[str] = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 , image.shape[0] ): for j in range(0 , image.shape[1] ): lowerCAmelCase__ : Dict = lbp.local_binary_value(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert lbp_image.any()
307
0
import importlib import math import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Tuple, Union import flax import jax.numpy as jnp from ..utils import BaseOutput lowerCamelCase__ = """scheduler_config.json""" class A__ ( __magic_name__ ): lowercase = 1 lowercase = 2 lowercase = 3 lowercase = 4 lowercase = 5 @dataclass class A__ ( __magic_name__ ): lowercase = 42 class A__ : lowercase = SCHEDULER_CONFIG_NAME lowercase = ["dtype"] lowercase = [] lowercase = True @classmethod def _lowerCamelCase ( cls : Any , a : Dict[str, Any] = None , a : Optional[str] = None , a : List[str]=False , **a : List[str] , ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = cls.load_config( pretrained_model_name_or_path=a , subfolder=a , return_unused_kwargs=a , **a , ) lowerCAmelCase__ , lowerCAmelCase__ : Optional[Any] = cls.from_config(a , return_unused_kwargs=a , **a ) if hasattr(a , 'create_state' ) and getattr(a , 'has_state' , a ): lowerCAmelCase__ : List[str] = scheduler.create_state() if return_unused_kwargs: return scheduler, state, unused_kwargs return scheduler, state def _lowerCamelCase ( self : List[str] , a : Union[str, os.PathLike] , a : bool = False , **a : List[Any] ): '''simple docstring''' self.save_config(save_directory=a , push_to_hub=a , **a ) @property def _lowerCamelCase ( self : Dict ): '''simple docstring''' return self._get_compatibles() @classmethod def _lowerCamelCase ( cls : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = list(set([cls.__name__] + cls._compatibles ) ) lowerCAmelCase__ : Any = importlib.import_module(__name__.split('.' )[0] ) lowerCAmelCase__ : Optional[int] = [ getattr(a , a ) for c in compatible_classes_str if hasattr(a , a ) ] return compatible_classes def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: assert len(SCREAMING_SNAKE_CASE_ ) >= x.ndim return jnp.broadcast_to(x.reshape(x.shape + (1,) * (len(SCREAMING_SNAKE_CASE_ ) - x.ndim) ) , SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=0.999 , SCREAMING_SNAKE_CASE_=jnp.floataa ) -> Optional[int]: def alpha_bar(SCREAMING_SNAKE_CASE_ ): return math.cos((time_step + 0.008) / 1.008 * math.pi / 2 ) ** 2 lowerCAmelCase__ : Optional[Any] = [] for i in range(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Dict = i / num_diffusion_timesteps lowerCAmelCase__ : str = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar(SCREAMING_SNAKE_CASE_ ) / alpha_bar(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) return jnp.array(SCREAMING_SNAKE_CASE_ , dtype=SCREAMING_SNAKE_CASE_ ) @flax.struct.dataclass class A__ : lowercase = 42 lowercase = 42 lowercase = 42 @classmethod def _lowerCamelCase ( cls : int , a : Dict ): '''simple docstring''' lowerCAmelCase__ : Dict = scheduler.config if config.trained_betas is not None: lowerCAmelCase__ : List[str] = jnp.asarray(config.trained_betas , dtype=scheduler.dtype ) elif config.beta_schedule == "linear": lowerCAmelCase__ : str = jnp.linspace(config.beta_start , config.beta_end , config.num_train_timesteps , dtype=scheduler.dtype ) elif config.beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowerCAmelCase__ : Dict = ( jnp.linspace( config.beta_start**0.5 , config.beta_end**0.5 , config.num_train_timesteps , dtype=scheduler.dtype ) ** 2 ) elif config.beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowerCAmelCase__ : Any = betas_for_alpha_bar(config.num_train_timesteps , dtype=scheduler.dtype ) else: raise NotImplementedError( f'''beta_schedule {config.beta_schedule} is not implemented for scheduler {scheduler.__class__.__name__}''' ) lowerCAmelCase__ : List[Any] = 1.0 - betas lowerCAmelCase__ : Union[str, Any] = jnp.cumprod(a , axis=0 ) return cls( alphas=a , betas=a , alphas_cumprod=a , ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: lowerCAmelCase__ : Union[str, Any] = state.alphas_cumprod lowerCAmelCase__ : Dict = alphas_cumprod[timesteps] ** 0.5 lowerCAmelCase__ : Optional[int] = sqrt_alpha_prod.flatten() lowerCAmelCase__ : Union[str, Any] = broadcast_to_shape_from_left(SCREAMING_SNAKE_CASE_ , original_samples.shape ) lowerCAmelCase__ : Tuple = (1 - alphas_cumprod[timesteps]) ** 0.5 lowerCAmelCase__ : int = sqrt_one_minus_alpha_prod.flatten() lowerCAmelCase__ : int = broadcast_to_shape_from_left(SCREAMING_SNAKE_CASE_ , original_samples.shape ) return sqrt_alpha_prod, sqrt_one_minus_alpha_prod def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: lowerCAmelCase__ , lowerCAmelCase__ : int = get_sqrt_alpha_prod(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : str = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: lowerCAmelCase__ , lowerCAmelCase__ : int = get_sqrt_alpha_prod(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Union[str, Any] = sqrt_alpha_prod * noise - sqrt_one_minus_alpha_prod * sample return velocity
353
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCamelCase__ = { """configuration_blip""": [ """BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BlipConfig""", """BlipTextConfig""", """BlipVisionConfig""", ], """processing_blip""": ["""BlipProcessor"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""BlipImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """BlipModel""", """BlipPreTrainedModel""", """BlipForConditionalGeneration""", """BlipForQuestionAnswering""", """BlipVisionModel""", """BlipTextModel""", """BlipForImageTextRetrieval""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFBlipModel""", """TFBlipPreTrainedModel""", """TFBlipForConditionalGeneration""", """TFBlipForQuestionAnswering""", """TFBlipVisionModel""", """TFBlipTextModel""", """TFBlipForImageTextRetrieval""", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: if not all(x.isalpha() for x in string ): raise ValueError('String must only contain alphabetic characters.' ) lowerCAmelCase__ : Tuple = sorted(string.lower() ) return len(SCREAMING_SNAKE_CASE_ ) == len(set(SCREAMING_SNAKE_CASE_ ) ) if __name__ == "__main__": lowerCamelCase__ = input("""Enter a string """).strip() lowerCamelCase__ = is_isogram(input_str) print(F"""{input_str} is {"an" if isogram else "not an"} isogram.""")
354
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=0.999 , SCREAMING_SNAKE_CASE_="cosine" , ) -> Union[str, Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(SCREAMING_SNAKE_CASE_ ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(SCREAMING_SNAKE_CASE_ ): return math.exp(t * -12.0 ) else: raise ValueError(F'''Unsupported alpha_tranform_type: {alpha_transform_type}''' ) lowerCAmelCase__ : Tuple = [] for i in range(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : List[Any] = i / num_diffusion_timesteps lowerCAmelCase__ : str = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) return torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.floataa ) class A__ ( __magic_name__ , __magic_name__ ): lowercase = [e.name for e in KarrasDiffusionSchedulers] lowercase = 2 @register_to_config def __init__( self : Union[str, Any] , a : int = 1_000 , a : float = 0.0_0_0_8_5 , a : float = 0.0_1_2 , a : str = "linear" , a : Optional[Union[np.ndarray, List[float]]] = None , a : str = "epsilon" , a : Optional[bool] = False , a : Optional[bool] = False , a : float = 1.0 , a : str = "linspace" , a : int = 0 , ): '''simple docstring''' if trained_betas is not None: lowerCAmelCase__ : List[str] = torch.tensor(a , dtype=torch.floataa ) elif beta_schedule == "linear": lowerCAmelCase__ : List[str] = torch.linspace(a , a , a , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowerCAmelCase__ : Union[str, Any] = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , a , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowerCAmelCase__ : int = betas_for_alpha_bar(a , alpha_transform_type='cosine' ) elif beta_schedule == "exp": lowerCAmelCase__ : List[str] = betas_for_alpha_bar(a , alpha_transform_type='exp' ) else: raise NotImplementedError(f'''{beta_schedule} does is not implemented for {self.__class__}''' ) lowerCAmelCase__ : int = 1.0 - self.betas lowerCAmelCase__ : Tuple = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(a , a , a ) lowerCAmelCase__ : Optional[Any] = use_karras_sigmas def _lowerCamelCase ( self : str , a : List[Any] , a : str=None ): '''simple docstring''' if schedule_timesteps is None: lowerCAmelCase__ : List[str] = self.timesteps lowerCAmelCase__ : int = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: lowerCAmelCase__ : List[str] = 1 if len(a ) > 1 else 0 else: lowerCAmelCase__ : Tuple = timestep.cpu().item() if torch.is_tensor(a ) else timestep lowerCAmelCase__ : Tuple = self._index_counter[timestep_int] return indices[pos].item() @property def _lowerCamelCase ( self : Dict ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _lowerCamelCase ( self : Tuple , a : torch.FloatTensor , a : Union[float, torch.FloatTensor] , ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.index_for_timestep(a ) lowerCAmelCase__ : Any = self.sigmas[step_index] lowerCAmelCase__ : Optional[Any] = sample / ((sigma**2 + 1) ** 0.5) return sample def _lowerCamelCase ( self : List[str] , a : int , a : Union[str, torch.device] = None , a : Optional[int] = None , ): '''simple docstring''' lowerCAmelCase__ : Any = num_inference_steps lowerCAmelCase__ : Union[str, Any] = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": lowerCAmelCase__ : Union[str, Any] = np.linspace(0 , num_train_timesteps - 1 , a , dtype=a )[::-1].copy() elif self.config.timestep_spacing == "leading": lowerCAmelCase__ : List[Any] = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : Dict = (np.arange(0 , a ) * step_ratio).round()[::-1].copy().astype(a ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": lowerCAmelCase__ : Tuple = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowerCAmelCase__ : int = (np.arange(a , 0 , -step_ratio )).round().copy().astype(a ) timesteps -= 1 else: raise ValueError( f'''{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.''' ) lowerCAmelCase__ : str = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) lowerCAmelCase__ : List[Any] = np.log(a ) lowerCAmelCase__ : Optional[int] = np.interp(a , np.arange(0 , len(a ) ) , a ) if self.config.use_karras_sigmas: lowerCAmelCase__ : str = self._convert_to_karras(in_sigmas=a , num_inference_steps=self.num_inference_steps ) lowerCAmelCase__ : Union[str, Any] = np.array([self._sigma_to_t(a , a ) for sigma in sigmas] ) lowerCAmelCase__ : Tuple = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) lowerCAmelCase__ : Dict = torch.from_numpy(a ).to(device=a ) lowerCAmelCase__ : Union[str, Any] = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) lowerCAmelCase__ : Tuple = torch.from_numpy(a ) lowerCAmelCase__ : List[str] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(a ).startswith('mps' ): # mps does not support float64 lowerCAmelCase__ : Optional[Any] = timesteps.to(a , dtype=torch.floataa ) else: lowerCAmelCase__ : Any = timesteps.to(device=a ) # empty dt and derivative lowerCAmelCase__ : str = None lowerCAmelCase__ : Optional[int] = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter lowerCAmelCase__ : Optional[Any] = defaultdict(a ) def _lowerCamelCase ( self : Any , a : Dict , a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.log(a ) # get distribution lowerCAmelCase__ : Tuple = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range lowerCAmelCase__ : Optional[int] = np.cumsum((dists >= 0) , axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) lowerCAmelCase__ : List[str] = low_idx + 1 lowerCAmelCase__ : List[str] = log_sigmas[low_idx] lowerCAmelCase__ : Any = log_sigmas[high_idx] # interpolate sigmas lowerCAmelCase__ : Union[str, Any] = (low - log_sigma) / (low - high) lowerCAmelCase__ : List[Any] = np.clip(a , 0 , 1 ) # transform interpolation to time range lowerCAmelCase__ : List[Any] = (1 - w) * low_idx + w * high_idx lowerCAmelCase__ : Any = t.reshape(sigma.shape ) return t def _lowerCamelCase ( self : Tuple , a : torch.FloatTensor , a : Any ): '''simple docstring''' lowerCAmelCase__ : float = in_sigmas[-1].item() lowerCAmelCase__ : float = in_sigmas[0].item() lowerCAmelCase__ : Tuple = 7.0 # 7.0 is the value used in the paper lowerCAmelCase__ : Tuple = np.linspace(0 , 1 , a ) lowerCAmelCase__ : Any = sigma_min ** (1 / rho) lowerCAmelCase__ : Optional[Any] = sigma_max ** (1 / rho) lowerCAmelCase__ : Tuple = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def _lowerCamelCase ( self : Any ): '''simple docstring''' return self.dt is None def _lowerCamelCase ( self : List[str] , a : Union[torch.FloatTensor, np.ndarray] , a : Union[float, torch.FloatTensor] , a : Union[torch.FloatTensor, np.ndarray] , a : bool = True , ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.index_for_timestep(a ) # advance index counter by 1 lowerCAmelCase__ : Tuple = timestep.cpu().item() if torch.is_tensor(a ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: lowerCAmelCase__ : Union[str, Any] = self.sigmas[step_index] lowerCAmelCase__ : Union[str, Any] = self.sigmas[step_index + 1] else: # 2nd order / Heun's method lowerCAmelCase__ : int = self.sigmas[step_index - 1] lowerCAmelCase__ : Any = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API lowerCAmelCase__ : Optional[int] = 0 lowerCAmelCase__ : Union[str, Any] = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": lowerCAmelCase__ : int = sigma_hat if self.state_in_first_order else sigma_next lowerCAmelCase__ : Any = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase__ : Dict = sigma_hat if self.state_in_first_order else sigma_next lowerCAmelCase__ : List[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": lowerCAmelCase__ : int = model_output else: raise ValueError( f'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`''' ) if self.config.clip_sample: lowerCAmelCase__ : str = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order lowerCAmelCase__ : Dict = (sample - pred_original_sample) / sigma_hat # 3. delta timestep lowerCAmelCase__ : Optional[int] = sigma_next - sigma_hat # store for 2nd order step lowerCAmelCase__ : List[Any] = derivative lowerCAmelCase__ : str = dt lowerCAmelCase__ : Dict = sample else: # 2. 2nd order / Heun's method lowerCAmelCase__ : Union[str, Any] = (sample - pred_original_sample) / sigma_next lowerCAmelCase__ : Union[str, Any] = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample lowerCAmelCase__ : Dict = self.dt lowerCAmelCase__ : Optional[int] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" lowerCAmelCase__ : List[str] = None lowerCAmelCase__ : Tuple = None lowerCAmelCase__ : str = None lowerCAmelCase__ : Tuple = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=a ) def _lowerCamelCase ( self : int , a : torch.FloatTensor , a : torch.FloatTensor , a : torch.FloatTensor , ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(a ): # mps does not support float64 lowerCAmelCase__ : Optional[int] = self.timesteps.to(original_samples.device , dtype=torch.floataa ) lowerCAmelCase__ : int = timesteps.to(original_samples.device , dtype=torch.floataa ) else: lowerCAmelCase__ : Union[str, Any] = self.timesteps.to(original_samples.device ) lowerCAmelCase__ : Optional[Any] = timesteps.to(original_samples.device ) lowerCAmelCase__ : List[Any] = [self.index_for_timestep(a , a ) for t in timesteps] lowerCAmelCase__ : List[str] = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): lowerCAmelCase__ : Any = sigma.unsqueeze(-1 ) lowerCAmelCase__ : List[str] = original_samples + noise * sigma return noisy_samples def __len__( self : int ): '''simple docstring''' return self.config.num_train_timesteps
307
0
from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record lowerCamelCase__ = """\\n@article{wang2019superglue,\n title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},\n author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},\n journal={arXiv preprint arXiv:1905.00537},\n year={2019}\n}\n""" lowerCamelCase__ = """\\nSuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after\nGLUE with a new set of more difficult language understanding tasks, improved\nresources, and a new public leaderboard.\n""" lowerCamelCase__ = """\nCompute SuperGLUE evaluation metric associated to each SuperGLUE dataset.\nArgs:\n predictions: list of predictions to score. Depending on the SuperGlUE subset:\n - for \'record\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'prediction_text\': the predicted answer text\n - for \'multirc\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question-answer pair as specified by the dataset\n - \'prediction\': the predicted answer label\n - otherwise: list of predicted labels\n references: list of reference labels. Depending on the SuperGLUE subset:\n - for \'record\': list of question-answers dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'answers\': list of possible answers\n - otherwise: list of reference labels\nReturns: depending on the SuperGLUE subset:\n - for \'record\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1\': F1 score\n - for \'multirc\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1_m\': Per-question macro-F1 score\n - \'f1_a\': Average F1 score over all answers\n - for \'axb\':\n \'matthews_correlation\': Matthew Correlation\n - for \'cb\':\n - \'accuracy\': Accuracy\n - \'f1\': F1 score\n - for all others:\n - \'accuracy\': Accuracy\nExamples:\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\') # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"]\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\')\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\')\n >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}]\n >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\')\n >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'matthews_correlation\': 1.0}\n""" def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: return float((preds == labels).mean() ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_="binary" ) -> List[str]: lowerCAmelCase__ : List[Any] = simple_accuracy(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[Any] = float(fa_score(y_true=SCREAMING_SNAKE_CASE_ , y_pred=SCREAMING_SNAKE_CASE_ , average=SCREAMING_SNAKE_CASE_ ) ) return { "accuracy": acc, "f1": fa, } def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: lowerCAmelCase__ : Dict = {} for id_pred, label in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : List[str] = F'''{id_pred["idx"]["paragraph"]}-{id_pred["idx"]["question"]}''' lowerCAmelCase__ : Dict = id_pred['prediction'] if question_id in question_map: question_map[question_id].append((pred, label) ) else: lowerCAmelCase__ : List[str] = [(pred, label)] lowerCAmelCase__ , lowerCAmelCase__ : str = [], [] for question, preds_labels in question_map.items(): lowerCAmelCase__ , lowerCAmelCase__ : Tuple = zip(*SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : List[str] = fa_score(y_true=SCREAMING_SNAKE_CASE_ , y_pred=SCREAMING_SNAKE_CASE_ , average='macro' ) fas.append(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = int(sum(pred == label for pred, label in preds_labels ) == len(SCREAMING_SNAKE_CASE_ ) ) ems.append(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = float(sum(SCREAMING_SNAKE_CASE_ ) / len(SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase__ : Any = sum(SCREAMING_SNAKE_CASE_ ) / len(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : List[Any] = float(fa_score(y_true=SCREAMING_SNAKE_CASE_ , y_pred=[id_pred['prediction'] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A__ ( datasets.Metric ): def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format='numpy' if not self.config_name == 'record' and not self.config_name == 'multirc' else None , ) def _lowerCamelCase ( self : str ): '''simple docstring''' if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "prediction_text": datasets.Value('string' ), }, "references": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "answers": datasets.Sequence(datasets.Value('string' ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value('int64' ), "paragraph": datasets.Value('int64' ), "question": datasets.Value('int64' ), }, "prediction": datasets.Value('int64' ), }, "references": datasets.Value('int64' ), } else: return { "predictions": datasets.Value('int64' ), "references": datasets.Value('int64' ), } def _lowerCamelCase ( self : str , a : Tuple , a : Dict ): '''simple docstring''' if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(UpperCamelCase_ , UpperCamelCase_ )} elif self.config_name == "cb": return acc_and_fa(UpperCamelCase_ , UpperCamelCase_ , fa_avg='macro' ) elif self.config_name == "record": lowerCAmelCase__ : List[str] = [ { 'qas': [ {'id': ref['idx']['query'], 'answers': [{'text': ans} for ans in ref['answers']]} for ref in references ] } ] lowerCAmelCase__ : Tuple = {pred['idx']['query']: pred['prediction_text'] for pred in predictions} return evaluate_record(UpperCamelCase_ , UpperCamelCase_ )[0] elif self.config_name == "multirc": return evaluate_multirc(UpperCamelCase_ , UpperCamelCase_ ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(UpperCamelCase_ , UpperCamelCase_ )} else: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' )
355
from __future__ import annotations import collections import tempfile import unittest import numpy as np from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import is_tf_available, is_vision_available from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_tf_bert import TFBertModelTester from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester from ..deit.test_modeling_tf_deit import TFDeiTModelTester from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester from ..vit.test_modeling_tf_vit import TFViTModelTester if is_tf_available(): from transformers import ( TFBertModel, TFCLIPVisionModel, TFDeiTModel, TFRobertaModel, TFVisionTextDualEncoderModel, TFViTModel, VisionTextDualEncoderConfig, ) if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: if isinstance(SCREAMING_SNAKE_CASE_ , collections.abc.Iterable ): return x return (x, x) @require_tf class A__ : def _lowerCamelCase ( self : List[Any] , a : List[str] , a : Optional[Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' pass def _lowerCamelCase ( self : Dict ): '''simple docstring''' pass def _lowerCamelCase ( self : Dict , a : int , a : str , a : List[Any] , a : Dict , a : List[str]=None , **a : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = VisionTextDualEncoderConfig.from_vision_text_configs(a , a ) lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel(a ) lowerCAmelCase__ : Tuple = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], config.projection_dim) ) def _lowerCamelCase ( self : Union[str, Any] , a : Dict , a : Tuple , a : Dict , a : Union[str, Any] , a : List[Any]=None , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : List[str] = self.get_vision_text_model(a , a ) lowerCAmelCase__ : List[Any] = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : Optional[int] = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def _lowerCamelCase ( self : List[str] , a : Optional[int] , a : Optional[int] , a : Union[str, Any] , a : List[Any] , a : Any=None , **a : Dict ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Optional[Any] = {'vision_model': vision_model, 'text_model': text_model} lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel.from_vision_text_pretrained(**a ) lowerCAmelCase__ : Union[str, Any] = model(input_ids=a , pixel_values=a , attention_mask=a ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def _lowerCamelCase ( self : Any , a : Optional[int] , a : Optional[int] , a : Dict , a : Optional[int] , a : Optional[int]=None , **a : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : int = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Dict = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : List[str] = model(input_ids=a , pixel_values=a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = output[0].numpy() with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(a ) lowerCAmelCase__ : Any = TFVisionTextDualEncoderModel.from_pretrained(a ) lowerCAmelCase__ : int = model(input_ids=a , pixel_values=a , attention_mask=a ) lowerCAmelCase__ : Union[str, Any] = after_output[0].numpy() lowerCAmelCase__ : Optional[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(a , 1E-5 ) def _lowerCamelCase ( self : List[str] , a : Dict , a : Optional[int] , a : List[Any] , a : str , a : int=None , **a : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Any = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : str = model( input_ids=a , pixel_values=a , attention_mask=a , output_attentions=a ) lowerCAmelCase__ : Union[str, Any] = output.vision_model_output.attentions self.assertEqual(len(a ) , vision_config.num_hidden_layers ) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCAmelCase__ : Optional[int] = to_atuple(vision_model.config.image_size ) lowerCAmelCase__ : Optional[Any] = to_atuple(vision_model.config.patch_size ) lowerCAmelCase__ : List[Any] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase__ : int = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCAmelCase__ : str = output.text_model_output.attentions self.assertEqual(len(a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _lowerCamelCase ( self : List[Any] , a : np.ndarray , a : np.ndarray , a : float ): '''simple docstring''' lowerCAmelCase__ : int = np.abs((a - b) ).max() self.assertLessEqual(a , a , f'''Difference between torch and flax is {diff} (>= {tol}).''' ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_model(**a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Any = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : str = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.prepare_config_and_inputs() self.check_save_load(**a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**a ) @slow def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = self.get_pretrained_model_and_inputs() lowerCAmelCase__ : List[Any] = model_a(**a ) lowerCAmelCase__ : Optional[int] = outputs[0].numpy() with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(a ) lowerCAmelCase__ : str = TFVisionTextDualEncoderModel.from_pretrained(a ) lowerCAmelCase__ : List[str] = model_a(**a ) lowerCAmelCase__ : int = after_outputs[0].numpy() lowerCAmelCase__ : List[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(a , 1E-5 ) @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-vit' , 'hf-internal-testing/tiny-random-bert' ) lowerCAmelCase__ : int = 13 lowerCAmelCase__ : List[Any] = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : int = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Optional[Any] = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : List[Any] = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : List[Any] , a : Dict , a : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = TFViTModel(a , name='vision_model' ) lowerCAmelCase__ : str = TFBertModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = TFViTModelTester(self ) lowerCAmelCase__ : Tuple = TFBertModelTester(self ) lowerCAmelCase__ : Optional[int] = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Union[str, Any] = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : int = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : str = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'Rocketknight1/tiny-random-deit-tf' , 'hf-internal-testing/tiny-random-roberta' ) lowerCAmelCase__ : Tuple = 13 lowerCAmelCase__ : Any = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : Dict = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Any = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : Tuple = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : str , a : Optional[Any] , a : Dict , a : Dict , a : Any , a : Any=None , **a : int ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Dict = self.get_vision_text_model(a , a ) lowerCAmelCase__ : Optional[int] = TFVisionTextDualEncoderModel(vision_model=a , text_model=a ) lowerCAmelCase__ : Any = model( input_ids=a , pixel_values=a , attention_mask=a , output_attentions=a ) lowerCAmelCase__ : Union[str, Any] = output.vision_model_output.attentions self.assertEqual(len(a ) , vision_config.num_hidden_layers ) # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) lowerCAmelCase__ : str = to_atuple(vision_model.config.image_size ) lowerCAmelCase__ : Union[str, Any] = to_atuple(vision_model.config.patch_size ) lowerCAmelCase__ : int = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase__ : int = num_patches + 2 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCAmelCase__ : List[str] = output.text_model_output.attentions self.assertEqual(len(a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _lowerCamelCase ( self : int , a : Optional[int] , a : int ): '''simple docstring''' lowerCAmelCase__ : Dict = TFDeiTModel(a , name='vision_model' ) lowerCAmelCase__ : List[Any] = TFRobertaModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = TFDeiTModelTester(self ) lowerCAmelCase__ : List[str] = TFRobertaModelTester(self ) lowerCAmelCase__ : str = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : List[Any] = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : List[str] = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : Any = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class A__ ( __magic_name__ , unittest.TestCase ): def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : int = TFVisionTextDualEncoderModel.from_vision_text_pretrained( 'Rocketknight1/tiny-random-clip-tf' , 'hf-internal-testing/tiny-random-bert' ) lowerCAmelCase__ : Dict = 13 lowerCAmelCase__ : str = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) lowerCAmelCase__ : List[Any] = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) lowerCAmelCase__ : Union[str, Any] = random_attention_mask([batch_size, 4] ) lowerCAmelCase__ : Optional[int] = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def _lowerCamelCase ( self : str , a : int , a : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = TFCLIPVisionModel(a , name='vision_model' ) lowerCAmelCase__ : List[str] = TFBertModel(a , name='text_model' ) return vision_model, text_model def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Any = TFCLIPVisionModelTester(self ) lowerCAmelCase__ : Union[str, Any] = TFBertModelTester(self ) lowerCAmelCase__ : Any = clip_model_tester.prepare_config_and_inputs() lowerCAmelCase__ : Any = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ : List[Any] = vision_config_and_inputs ( ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ( lowerCAmelCase__ ) , ) : str = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_vision @require_tf class A__ ( unittest.TestCase ): @slow def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Tuple = TFVisionTextDualEncoderModel.from_pretrained( 'clip-italian/clip-italian' , logit_scale_init_value=1.0 , from_pt=a ) lowerCAmelCase__ : List[Any] = VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian' ) lowerCAmelCase__ : int = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) lowerCAmelCase__ : Any = processor( text=['una foto di un gatto', 'una foto di un cane'] , images=a , padding=a , return_tensors='np' ) lowerCAmelCase__ : Union[str, Any] = model(**a ) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) ) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) lowerCAmelCase__ : List[str] = np.array([[1.2_2_8_4_7_2_7, 0.3_1_0_4_1_2_2]] ) self.assertTrue(np.allclose(outputs.logits_per_image.numpy() , a , atol=1E-3 ) )
307
0
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = [[1, 2, 4], [1, 2, 3, 4]] lowerCAmelCase__ : Tuple = DisjunctiveConstraint(__lowerCamelCase ) self.assertTrue(isinstance(dc.token_ids , __lowerCamelCase ) ) with self.assertRaises(__lowerCamelCase ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(__lowerCamelCase ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : List[Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(__lowerCamelCase ): DisjunctiveConstraint(__lowerCamelCase ) # fails here def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Any = [[1, 2, 3], [1, 2, 4]] lowerCAmelCase__ : Dict = DisjunctiveConstraint(__lowerCamelCase ) lowerCAmelCase__ : List[str] = dc.update(1 ) lowerCAmelCase__ : str = stepped is True and completed is False and reset is False self.assertTrue(__lowerCamelCase ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) lowerCAmelCase__ : Any = dc.update(2 ) lowerCAmelCase__ : Union[str, Any] = stepped is True and completed is False and reset is False self.assertTrue(__lowerCamelCase ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) lowerCAmelCase__ : List[Any] = dc.update(3 ) lowerCAmelCase__ : List[str] = stepped is True and completed is True and reset is False self.assertTrue(__lowerCamelCase ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] lowerCAmelCase__ : Any = DisjunctiveConstraint(__lowerCamelCase ) lowerCAmelCase__ : int = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) lowerCAmelCase__ : Union[str, Any] = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) lowerCAmelCase__ : Tuple = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) lowerCAmelCase__ : Dict = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() lowerCAmelCase__ : Optional[Any] = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) lowerCAmelCase__ : Union[str, Any] = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) lowerCAmelCase__ : Union[str, Any] = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
356
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) lowerCamelCase__ = logging.getLogger(__name__) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : Dict = np.argmax(SCREAMING_SNAKE_CASE_ , axis=1 ) return np.sum(outputs == labels ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: with open(SCREAMING_SNAKE_CASE_ , encoding='utf_8' ) as f: lowerCAmelCase__ : Dict = csv.reader(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = [] next(SCREAMING_SNAKE_CASE_ ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE_ ): output.append((' '.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: lowerCAmelCase__ : Dict = [] for dataset in encoded_datasets: lowerCAmelCase__ : List[str] = len(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase__ : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase__ : List[Any] = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) lowerCAmelCase__ : Tuple = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase__ : Any = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase__ : Optional[Any] = with_conta lowerCAmelCase__ : List[str] = with_conta lowerCAmelCase__ : List[Any] = len(SCREAMING_SNAKE_CASE_ ) - 1 lowerCAmelCase__ : Tuple = len(SCREAMING_SNAKE_CASE_ ) - 1 lowerCAmelCase__ : Tuple = with_conta lowerCAmelCase__ : Optional[int] = with_conta lowerCAmelCase__ : Optional[int] = mc_label lowerCAmelCase__ : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE_ ) for t in all_inputs ) ) return tensor_datasets def lowerCAmelCase__ ( ) -> int: lowerCAmelCase__ : int = argparse.ArgumentParser() parser.add_argument('--model_name' , type=SCREAMING_SNAKE_CASE_ , default='openai-gpt' , help='pretrained model name' ) parser.add_argument('--do_train' , action='store_true' , help='Whether to run training.' ) parser.add_argument('--do_eval' , action='store_true' , help='Whether to run eval on the dev set.' ) parser.add_argument( '--output_dir' , default=SCREAMING_SNAKE_CASE_ , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , help='The output directory where the model predictions and checkpoints will be written.' , ) parser.add_argument('--train_dataset' , type=SCREAMING_SNAKE_CASE_ , default='' ) parser.add_argument('--eval_dataset' , type=SCREAMING_SNAKE_CASE_ , default='' ) parser.add_argument('--seed' , type=SCREAMING_SNAKE_CASE_ , default=42 ) parser.add_argument('--num_train_epochs' , type=SCREAMING_SNAKE_CASE_ , default=3 ) parser.add_argument('--train_batch_size' , type=SCREAMING_SNAKE_CASE_ , default=8 ) parser.add_argument('--eval_batch_size' , type=SCREAMING_SNAKE_CASE_ , default=16 ) parser.add_argument('--adam_epsilon' , default=1e-8 , type=SCREAMING_SNAKE_CASE_ , help='Epsilon for Adam optimizer.' ) parser.add_argument('--max_grad_norm' , type=SCREAMING_SNAKE_CASE_ , default=1 ) parser.add_argument( '--max_steps' , default=-1 , type=SCREAMING_SNAKE_CASE_ , help=( 'If > 0: set total number of training steps to perform. Override num_train_epochs.' ) , ) parser.add_argument( '--gradient_accumulation_steps' , type=SCREAMING_SNAKE_CASE_ , default=1 , help='Number of updates steps to accumulate before performing a backward/update pass.' , ) parser.add_argument('--learning_rate' , type=SCREAMING_SNAKE_CASE_ , default=6.25e-5 ) parser.add_argument('--warmup_steps' , default=0 , type=SCREAMING_SNAKE_CASE_ , help='Linear warmup over warmup_steps.' ) parser.add_argument('--lr_schedule' , type=SCREAMING_SNAKE_CASE_ , default='warmup_linear' ) parser.add_argument('--weight_decay' , type=SCREAMING_SNAKE_CASE_ , default=0.01 ) parser.add_argument('--lm_coef' , type=SCREAMING_SNAKE_CASE_ , default=0.9 ) parser.add_argument('--n_valid' , type=SCREAMING_SNAKE_CASE_ , default=374 ) parser.add_argument('--server_ip' , type=SCREAMING_SNAKE_CASE_ , default='' , help='Can be used for distant debugging.' ) parser.add_argument('--server_port' , type=SCREAMING_SNAKE_CASE_ , default='' , help='Can be used for distant debugging.' ) lowerCAmelCase__ : List[str] = parser.parse_args() print(SCREAMING_SNAKE_CASE_ ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('Waiting for debugger attach' ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=SCREAMING_SNAKE_CASE_ ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) lowerCAmelCase__ : str = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) lowerCAmelCase__ : Dict = torch.cuda.device_count() logger.info('device: {}, n_gpu {}'.format(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) if not args.do_train and not args.do_eval: raise ValueError('At least one of `do_train` or `do_eval` must be True.' ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset lowerCAmelCase__ : Union[str, Any] = ['_start_', '_delimiter_', '_classify_'] lowerCAmelCase__ : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE_ ) ) model.to(SCREAMING_SNAKE_CASE_ ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE_ ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) ) elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE_ ) for o in obj] logger.info('Encoding dataset...' ) lowerCAmelCase__ : List[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase__ : str = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase__ : Union[str, Any] = (train_dataset, eval_dataset) lowerCAmelCase__ : List[str] = tokenize_and_encode(SCREAMING_SNAKE_CASE_ ) # Compute the max input length for the Transformer lowerCAmelCase__ : Union[str, Any] = model.config.n_positions // 2 - 2 lowerCAmelCase__ : Tuple = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) lowerCAmelCase__ : Dict = min(SCREAMING_SNAKE_CASE_ , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase__ : int = pre_process_datasets(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase__ : str = TensorDataset(*SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = RandomSampler(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Tuple = DataLoader(SCREAMING_SNAKE_CASE_ , sampler=SCREAMING_SNAKE_CASE_ , batch_size=args.train_batch_size ) lowerCAmelCase__ : Optional[Any] = TensorDataset(*SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = SequentialSampler(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = DataLoader(SCREAMING_SNAKE_CASE_ , sampler=SCREAMING_SNAKE_CASE_ , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase__ : Union[str, Any] = args.max_steps lowerCAmelCase__ : int = args.max_steps // (len(SCREAMING_SNAKE_CASE_ ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase__ : Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase__ : Optional[int] = list(model.named_parameters() ) lowerCAmelCase__ : Tuple = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] lowerCAmelCase__ : str = [ { 'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], 'weight_decay': args.weight_decay, }, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], 'weight_decay': 0.0}, ] lowerCAmelCase__ : Union[str, Any] = AdamW(SCREAMING_SNAKE_CASE_ , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase__ : int = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE_ , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE_ ) if args.do_train: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc='Epoch' ): lowerCAmelCase__ : str = 0 lowerCAmelCase__ : int = 0 lowerCAmelCase__ : str = tqdm(SCREAMING_SNAKE_CASE_ , desc='Training' ) for step, batch in enumerate(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Union[str, Any] = tuple(t.to(SCREAMING_SNAKE_CASE_ ) for t in batch ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Any = batch lowerCAmelCase__ : Tuple = model(SCREAMING_SNAKE_CASE_ , mc_token_ids=SCREAMING_SNAKE_CASE_ , lm_labels=SCREAMING_SNAKE_CASE_ , mc_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : str = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase__ : Optional[int] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase__ : List[str] = 'Training loss: {:.2e} lr: {:.2e}'.format(SCREAMING_SNAKE_CASE_ , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase__ : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE_ , 'module' ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase__ : List[str] = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : List[str] = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE_ ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE_ ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase__ : Dict = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase__ : List[Any] = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE_ ) if args.do_eval: model.eval() lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = 0, 0 lowerCAmelCase__ , lowerCAmelCase__ : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE_ , desc='Evaluating' ): lowerCAmelCase__ : str = tuple(t.to(SCREAMING_SNAKE_CASE_ ) for t in batch ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Tuple = batch with torch.no_grad(): lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : List[str] = model( SCREAMING_SNAKE_CASE_ , mc_token_ids=SCREAMING_SNAKE_CASE_ , lm_labels=SCREAMING_SNAKE_CASE_ , mc_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Any = mc_logits.detach().cpu().numpy() lowerCAmelCase__ : List[Any] = mc_labels.to('cpu' ).numpy() lowerCAmelCase__ : str = accuracy(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase__ : Optional[int] = eval_loss / nb_eval_steps lowerCAmelCase__ : Any = eval_accuracy / nb_eval_examples lowerCAmelCase__ : Union[str, Any] = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase__ : Tuple = {'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'train_loss': train_loss} lowerCAmelCase__ : Dict = os.path.join(args.output_dir , 'eval_results.txt' ) with open(SCREAMING_SNAKE_CASE_ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key in sorted(result.keys() ): logger.info(' %s = %s' , SCREAMING_SNAKE_CASE_ , str(result[key] ) ) writer.write('%s = %s\n' % (key, str(result[key] )) ) if __name__ == "__main__": main()
307
0
from __future__ import annotations from math import pow, sqrt def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> dict[str, float]: if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError('One and only one argument must be 0' ) if resistance == 0: return {"resistance": sqrt(pow(UpperCAmelCase_ , 2 ) - pow(UpperCAmelCase_ , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(UpperCAmelCase_ , 2 ) - pow(UpperCAmelCase_ , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(UpperCAmelCase_ , 2 ) + pow(UpperCAmelCase_ , 2 ) )} else: raise ValueError('Exactly one argument must be 0' ) if __name__ == "__main__": import doctest doctest.testmod()
357
import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers lowerCamelCase__ = """python tqdm regex requests packaging filelock numpy tokenizers""".split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append("""dataclasses""") if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append("""importlib_metadata""") for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F"""can't find {pkg} in {deps.keys()}, check dependency_versions_table.py""") def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> int: require_version(deps[pkg] , SCREAMING_SNAKE_CASE_ )
307
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ = {'configuration_ibert': ['IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'IBertConfig', 'IBertOnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ 'IBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'IBertForMaskedLM', 'IBertForMultipleChoice', 'IBertForQuestionAnswering', 'IBertForSequenceClassification', 'IBertForTokenClassification', 'IBertModel', 'IBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBertConfig, IBertOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ibert import ( IBERT_PRETRAINED_MODEL_ARCHIVE_LIST, IBertForMaskedLM, IBertForMultipleChoice, IBertForQuestionAnswering, IBertForSequenceClassification, IBertForTokenClassification, IBertModel, IBertPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
358
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class A__ ( __magic_name__ ): lowercase = (DPMSolverSDEScheduler,) lowercase = 10 def _lowerCamelCase ( self : Optional[int] , **a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = { 'num_train_timesteps': 1_100, 'beta_start': 0.0_0_0_1, 'beta_end': 0.0_2, 'beta_schedule': 'linear', 'noise_sampler_seed': 0, } config.update(**a ) return config def _lowerCamelCase ( self : Tuple ): '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=a ) def _lowerCamelCase ( self : int ): '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=a , beta_end=a ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=a ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = self.scheduler_classes[0] lowerCAmelCase__ : Tuple = self.get_scheduler_config() lowerCAmelCase__ : List[Any] = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Dict = self.dummy_model() lowerCAmelCase__ : int = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : int = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : List[Any] = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : int = scheduler.step(a , a , a ) lowerCAmelCase__ : Any = output.prev_sample lowerCAmelCase__ : List[Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Optional[int] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_7_8_2_1_0_4_4_9_2_1_8_7_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_7_8_7_0_5_9_6_4_5_6_5_2_7_7 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_2_1_1_1_8_1_6_4_0_6 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_6_8_9_2_2_9_9_6_5_2 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.scheduler_classes[0] lowerCAmelCase__ : List[str] = self.get_scheduler_config(prediction_type='v_prediction' ) lowerCAmelCase__ : Any = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Optional[int] = self.dummy_model() lowerCAmelCase__ : Union[str, Any] = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : Any = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : str = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : str = model(a , a ) lowerCAmelCase__ : Dict = scheduler.step(a , a , a ) lowerCAmelCase__ : Tuple = output.prev_sample lowerCAmelCase__ : int = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Union[str, Any] = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_2_4.7_7_1_4_9_2_0_0_4_3_9_4_5_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_2_2_6_2_8_9_0_1_4_8_1_6_2_8_4 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_2_8.1_6_6_3_3_6_0_5_9_5_7_0_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_6_8_8_3_2_6_0_0_1_1_6_7_2_9_7 ) < 1E-3 else: assert abs(result_sum.item() - 1_1_9.8_4_8_7_5_4_8_8_2_8_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.1_5_6_0_5_3_0_6_6_2_5_3_6_6_2_1 ) < 1E-3 def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = self.scheduler_classes[0] lowerCAmelCase__ : Optional[int] = self.get_scheduler_config() lowerCAmelCase__ : int = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : Tuple = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: lowerCAmelCase__ : Dict = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : Optional[int] = model(a , a ) lowerCAmelCase__ : Tuple = scheduler.step(a , a , a ) lowerCAmelCase__ : Dict = output.prev_sample lowerCAmelCase__ : Union[str, Any] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Dict = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_6_9_5_7_3_9_7_4_6_0_9_3_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_8_0_5_9_3_4_6_0_7_9_8_2_6_3_5 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_3_6_3_7_6_9_5_3_1_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_8_3_8_2_4_1_5_7_7_1 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.scheduler_classes[0] lowerCAmelCase__ : Any = self.get_scheduler_config() lowerCAmelCase__ : Any = scheduler_class(**a , use_karras_sigmas=a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) lowerCAmelCase__ : str = self.dummy_model() lowerCAmelCase__ : Any = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma lowerCAmelCase__ : str = sample.to(a ) for t in scheduler.timesteps: lowerCAmelCase__ : Any = scheduler.scale_model_input(a , a ) lowerCAmelCase__ : int = model(a , a ) lowerCAmelCase__ : Union[str, Any] = scheduler.step(a , a , a ) lowerCAmelCase__ : Union[str, Any] = output.prev_sample lowerCAmelCase__ : Optional[int] = torch.sum(torch.abs(a ) ) lowerCAmelCase__ : Any = torch.mean(torch.abs(a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_7_6.6_6_9_7_4_1_3_5_7_4_2_1_8_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_7.6_3_6_5_3_5_6_4_4_5_3_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 else: assert abs(result_sum.item() - 1_7_0.3_1_3_5_2_2_3_3_8_8_6_7_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2
307
0
from __future__ import annotations import numpy as np def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: return np.maximum(0 , lowerCAmelCase__ ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
359
import os import string import sys lowerCamelCase__ = 1 << 8 lowerCamelCase__ = { """tab""": ord("""\t"""), """newline""": ord("""\r"""), """esc""": 27, """up""": 65 + ARROW_KEY_FLAG, """down""": 66 + ARROW_KEY_FLAG, """right""": 67 + ARROW_KEY_FLAG, """left""": 68 + ARROW_KEY_FLAG, """mod_int""": 91, """undefined""": sys.maxsize, """interrupt""": 3, """insert""": 50, """delete""": 51, """pg_up""": 53, """pg_down""": 54, } lowerCamelCase__ = KEYMAP["""up"""] lowerCamelCase__ = KEYMAP["""left"""] if sys.platform == "win32": lowerCamelCase__ = [] lowerCamelCase__ = { b"""\xe0H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\x00H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\xe0P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\x00P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\xe0M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\x00M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\xe0K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, b"""\x00K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, } for i in range(10): lowerCamelCase__ = ord(str(i)) def lowerCAmelCase__ ( ) -> Dict: if os.name == "nt": import msvcrt lowerCAmelCase__ : Dict = 'mbcs' # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(SCREAMING_SNAKE_CASE_ ) == 0: # Read the keystroke lowerCAmelCase__ : Optional[Any] = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): lowerCAmelCase__ : Dict = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: lowerCAmelCase__ : Dict = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP['mod_int'] ) ) WIN_CH_BUFFER.append(SCREAMING_SNAKE_CASE_ ) if ord(SCREAMING_SNAKE_CASE_ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) lowerCAmelCase__ : Dict = chr(KEYMAP['esc'] ) except KeyError: lowerCAmelCase__ : Dict = cha[1] else: lowerCAmelCase__ : List[Any] = ch.decode(SCREAMING_SNAKE_CASE_ ) else: lowerCAmelCase__ : Tuple = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty lowerCAmelCase__ : Tuple = sys.stdin.fileno() lowerCAmelCase__ : Any = termios.tcgetattr(SCREAMING_SNAKE_CASE_ ) try: tty.setraw(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = sys.stdin.read(1 ) finally: termios.tcsetattr(SCREAMING_SNAKE_CASE_ , termios.TCSADRAIN , SCREAMING_SNAKE_CASE_ ) return ch def lowerCAmelCase__ ( ) -> Union[str, Any]: lowerCAmelCase__ : Any = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["esc"]: lowerCAmelCase__ : Union[str, Any] = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) == KEYMAP["mod_int"]: lowerCAmelCase__ : str = get_raw_chars() if ord(SCREAMING_SNAKE_CASE_ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(SCREAMING_SNAKE_CASE_ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(SCREAMING_SNAKE_CASE_ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
307
0
"""simple docstring""" import hashlib import unittest from typing import Dict import numpy as np from transformers import ( MODEL_FOR_MASK_GENERATION_MAPPING, TF_MODEL_FOR_MASK_GENERATION_MAPPING, is_vision_available, pipeline, ) from transformers.pipelines import MaskGenerationPipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) if is_vision_available(): from PIL import Image else: class A__ : @staticmethod def _lowerCamelCase ( *a : Union[str, Any] , **a : Any ): '''simple docstring''' pass def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : Tuple = hashlib.mda(image.tobytes() ) return m.hexdigest()[:10] def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Dict: lowerCAmelCase__ : Optional[Any] = np.array(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[int] = npimg.shape return {"hash": hashimage(SCREAMING_SNAKE_CASE_ ), "shape": shape} @is_pipeline_test @require_vision @require_torch class A__ ( unittest.TestCase ): lowercase = dict( (list(MODEL_FOR_MASK_GENERATION_MAPPING.items() ) if MODEL_FOR_MASK_GENERATION_MAPPING else []) ) lowercase = dict( (list(TF_MODEL_FOR_MASK_GENERATION_MAPPING.items() ) if TF_MODEL_FOR_MASK_GENERATION_MAPPING else []) ) def _lowerCamelCase ( self : List[str] , a : Optional[int] , a : str , a : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Any = MaskGenerationPipeline(model=a__ , image_processor=a__ ) return image_segmenter, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def _lowerCamelCase ( self : str , a : List[Any] , a : str ): '''simple docstring''' pass @require_tf @unittest.skip('Image segmentation not implemented in TF' ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' pass @slow @require_torch def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = pipeline('mask-generation' , model='facebook/sam-vit-huge' ) lowerCAmelCase__ : Optional[Any] = image_segmenter('http://images.cocodataset.org/val2017/000000039769.jpg' , points_per_batch=256 ) # Shortening by hashing lowerCAmelCase__ : Union[str, Any] = [] for i, o in enumerate(outputs['masks'] ): new_outupt += [{"mask": mask_to_test_readable(a__ ), "scores": outputs["scores"][i]}] # fmt: off self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'mask': {'hash': '115ad19f5f', 'shape': (480, 640)}, 'scores': 1.0_4_4_4}, {'mask': {'hash': '6affa964c6', 'shape': (480, 640)}, 'scores': 1.0_2_1}, {'mask': {'hash': 'dfe28a0388', 'shape': (480, 640)}, 'scores': 1.0_1_6_7}, {'mask': {'hash': 'c0a5f4a318', 'shape': (480, 640)}, 'scores': 1.0_1_3_2}, {'mask': {'hash': 'fe8065c197', 'shape': (480, 640)}, 'scores': 1.0_0_5_3}, {'mask': {'hash': 'e2d0b7a0b7', 'shape': (480, 640)}, 'scores': 0.9_9_6_7}, {'mask': {'hash': '453c7844bd', 'shape': (480, 640)}, 'scores': 0.9_9_3}, {'mask': {'hash': '3d44f2926d', 'shape': (480, 640)}, 'scores': 0.9_9_0_9}, {'mask': {'hash': '64033ddc3f', 'shape': (480, 640)}, 'scores': 0.9_8_7_9}, {'mask': {'hash': '801064ff79', 'shape': (480, 640)}, 'scores': 0.9_8_3_4}, {'mask': {'hash': '6172f276ef', 'shape': (480, 640)}, 'scores': 0.9_7_1_6}, {'mask': {'hash': 'b49e60e084', 'shape': (480, 640)}, 'scores': 0.9_6_1_2}, {'mask': {'hash': 'a811e775fd', 'shape': (480, 640)}, 'scores': 0.9_5_9_9}, {'mask': {'hash': 'a6a8ebcf4b', 'shape': (480, 640)}, 'scores': 0.9_5_5_2}, {'mask': {'hash': '9d8257e080', 'shape': (480, 640)}, 'scores': 0.9_5_3_2}, {'mask': {'hash': '32de6454a8', 'shape': (480, 640)}, 'scores': 0.9_5_1_6}, {'mask': {'hash': 'af3d4af2c8', 'shape': (480, 640)}, 'scores': 0.9_4_9_9}, {'mask': {'hash': '3c6db475fb', 'shape': (480, 640)}, 'scores': 0.9_4_8_3}, {'mask': {'hash': 'c290813fb9', 'shape': (480, 640)}, 'scores': 0.9_4_6_4}, {'mask': {'hash': 'b6f0b8f606', 'shape': (480, 640)}, 'scores': 0.9_4_3}, {'mask': {'hash': '92ce16bfdf', 'shape': (480, 640)}, 'scores': 0.9_4_3}, {'mask': {'hash': 'c749b25868', 'shape': (480, 640)}, 'scores': 0.9_4_0_8}, {'mask': {'hash': 'efb6cab859', 'shape': (480, 640)}, 'scores': 0.9_3_3_5}, {'mask': {'hash': '1ff2eafb30', 'shape': (480, 640)}, 'scores': 0.9_3_2_6}, {'mask': {'hash': '788b798e24', 'shape': (480, 640)}, 'scores': 0.9_2_6_2}, {'mask': {'hash': 'abea804f0e', 'shape': (480, 640)}, 'scores': 0.8_9_9_9}, {'mask': {'hash': '7b9e8ddb73', 'shape': (480, 640)}, 'scores': 0.8_9_8_6}, {'mask': {'hash': 'cd24047c8a', 'shape': (480, 640)}, 'scores': 0.8_9_8_4}, {'mask': {'hash': '6943e6bcbd', 'shape': (480, 640)}, 'scores': 0.8_8_7_3}, {'mask': {'hash': 'b5f47c9191', 'shape': (480, 640)}, 'scores': 0.8_8_7_1} ] , ) # fmt: on @require_torch @slow def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : str = 'facebook/sam-vit-huge' lowerCAmelCase__ : Tuple = pipeline('mask-generation' , model=a__ ) lowerCAmelCase__ : Union[str, Any] = image_segmenter( 'http://images.cocodataset.org/val2017/000000039769.jpg' , pred_iou_thresh=1 , points_per_batch=256 ) # Shortening by hashing lowerCAmelCase__ : Any = [] for i, o in enumerate(outputs['masks'] ): new_outupt += [{"mask": mask_to_test_readable(a__ ), "scores": outputs["scores"][i]}] self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'mask': {'hash': '115ad19f5f', 'shape': (480, 640)}, 'scores': 1.0_4_4_4}, {'mask': {'hash': '6affa964c6', 'shape': (480, 640)}, 'scores': 1.0_2_1_0}, {'mask': {'hash': 'dfe28a0388', 'shape': (480, 640)}, 'scores': 1.0_1_6_7}, {'mask': {'hash': 'c0a5f4a318', 'shape': (480, 640)}, 'scores': 1.0_1_3_2}, {'mask': {'hash': 'fe8065c197', 'shape': (480, 640)}, 'scores': 1.0_0_5_3}, ] , )
360
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: return "".join([hex(SCREAMING_SNAKE_CASE_ )[2:].zfill(2 ).upper() for byte in list(SCREAMING_SNAKE_CASE_ )] ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> bytes: # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(SCREAMING_SNAKE_CASE_ ) % 2) != 0: raise ValueError( 'Base16 encoded data is invalid:\nData does not have an even number of hex digits.' ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(SCREAMING_SNAKE_CASE_ ) <= set('0123456789ABCDEF' ): raise ValueError( 'Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.' ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
307
0
import tempfile import unittest from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from transformers.testing_utils import ( is_torch_available, require_optimum, require_torch, slow, ) if is_torch_available(): import torch @require_torch @require_optimum @slow class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Any = '''hf-internal-testing/tiny-random-t5''' lowerCAmelCase__ : str = AutoTokenizer.from_pretrained(__A ) lowerCAmelCase__ : Optional[int] = AutoModelForSeqaSeqLM.from_pretrained(__A ) lowerCAmelCase__ : List[Any] = tokenizer('This is me' , return_tensors='pt' ) lowerCAmelCase__ : Optional[int] = model.to_bettertransformer() self.assertTrue(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules() ) ) lowerCAmelCase__ : List[str] = model.generate(**__A ) lowerCAmelCase__ : Optional[Any] = model.reverse_bettertransformer() self.assertFalse(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules() ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(__A ) lowerCAmelCase__ : List[Any] = AutoModelForSeqaSeqLM.from_pretrained(__A ) self.assertFalse( any('BetterTransformer' in mod.__class__.__name__ for _, mod in model_reloaded.named_modules() ) ) lowerCAmelCase__ : Optional[int] = model_reloaded.generate(**__A ) self.assertTrue(torch.allclose(__A , __A ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : List[Any] = '''hf-internal-testing/tiny-random-t5''' lowerCAmelCase__ : List[str] = AutoModelForSeqaSeqLM.from_pretrained(__A ) lowerCAmelCase__ : int = model.to_bettertransformer() with tempfile.TemporaryDirectory() as tmpdirname: with self.assertRaises(__A ): model.save_pretrained(__A ) lowerCAmelCase__ : Tuple = model.reverse_bettertransformer() model.save_pretrained(__A )
361
from __future__ import annotations def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[list[int]]: lowerCAmelCase__ : list[list[int]] = [] create_all_state(1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , [] , SCREAMING_SNAKE_CASE_ ) return result def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) -> None: if level == 0: total_list.append(current_list[:] ) return for i in range(SCREAMING_SNAKE_CASE_ , total_number - level + 2 ): current_list.append(SCREAMING_SNAKE_CASE_ ) create_all_state(i + 1 , SCREAMING_SNAKE_CASE_ , level - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) current_list.pop() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> None: for i in total_list: print(*SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCamelCase__ = 4 lowerCamelCase__ = 2 lowerCamelCase__ = generate_all_combinations(n, k) print_all_state(total_list)
307
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """uw-madison/mra-base-512-4""": """https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json""", } class A__ ( __magic_name__ ): lowercase = 'mra' def __init__( self : str , a : Optional[Any]=50_265 , a : List[Any]=768 , a : List[Any]=12 , a : Union[str, Any]=12 , a : Optional[int]=3_072 , a : Union[str, Any]="gelu" , a : List[str]=0.1 , a : str=0.1 , a : Any=512 , a : List[Any]=1 , a : Any=0.0_2 , a : str=1E-5 , a : str="absolute" , a : int=4 , a : Optional[int]="full" , a : Union[str, Any]=0 , a : int=0 , a : Optional[int]=1 , a : Tuple=0 , a : str=2 , **a : Any , ): '''simple docstring''' super().__init__(pad_token_id=__a , bos_token_id=__a , eos_token_id=__a , **__a ) lowerCAmelCase__ : List[str] = vocab_size lowerCAmelCase__ : str = max_position_embeddings lowerCAmelCase__ : Any = hidden_size lowerCAmelCase__ : Any = num_hidden_layers lowerCAmelCase__ : Optional[int] = num_attention_heads lowerCAmelCase__ : Dict = intermediate_size lowerCAmelCase__ : Union[str, Any] = hidden_act lowerCAmelCase__ : Optional[Any] = hidden_dropout_prob lowerCAmelCase__ : str = attention_probs_dropout_prob lowerCAmelCase__ : Optional[Any] = initializer_range lowerCAmelCase__ : List[Any] = type_vocab_size lowerCAmelCase__ : Optional[Any] = layer_norm_eps lowerCAmelCase__ : Any = position_embedding_type lowerCAmelCase__ : List[Any] = block_per_row lowerCAmelCase__ : Optional[int] = approx_mode lowerCAmelCase__ : Optional[int] = initial_prior_first_n_blocks lowerCAmelCase__ : Optional[Any] = initial_prior_diagonal_n_blocks
362
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class A__ ( unittest.TestCase ): @parameterized.expand([(None,), ('foo.json',)] ) def _lowerCamelCase ( self : Dict , a : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a , config_name=a ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(a , config_name=a ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 50 ) self.assertEqual(loaded_config.max_length , 20 ) self.assertEqual(loaded_config.max_time , a ) def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Dict = AutoConfig.from_pretrained('gpt2' ) lowerCAmelCase__ : Any = GenerationConfig.from_model_config(a ) lowerCAmelCase__ : Any = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(a , a ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : Dict = { 'max_new_tokens': 1_024, 'foo': 'bar', } lowerCAmelCase__ : List[Any] = copy.deepcopy(a ) lowerCAmelCase__ : Dict = generation_config.update(**a ) # update_kwargs was not modified (no side effects) self.assertEqual(a , a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_024 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(a , {'foo': 'bar'} ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = GenerationConfig() lowerCAmelCase__ : List[Any] = 'bar' with tempfile.TemporaryDirectory('test-generation-config' ) as tmp_dir: generation_config.save_pretrained(a ) lowerCAmelCase__ : List[Any] = GenerationConfig.from_pretrained(a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , 'bar' ) lowerCAmelCase__ : int = GenerationConfig.from_model_config(a ) assert not hasattr(a , 'foo' ) # no new kwargs should be initialized if from config def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , a ) self.assertEqual(default_config.num_beams , 1 ) lowerCAmelCase__ : List[Any] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , a ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(a , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class A__ ( unittest.TestCase ): @classmethod def _lowerCamelCase ( cls : int ): '''simple docstring''' lowerCAmelCase__ : List[str] = TOKEN HfFolder.save_token(a ) @classmethod def _lowerCamelCase ( cls : Optional[int] ): '''simple docstring''' try: delete_repo(token=cls._token , repo_id='test-generation-config' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-generation-config-org' ) except HTTPError: pass def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('test-generation-config' , use_auth_token=self._token ) lowerCAmelCase__ : Any = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='test-generation-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='test-generation-config' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : Tuple = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('valid_org/test-generation-config-org' , use_auth_token=self._token ) lowerCAmelCase__ : Dict = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-generation-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='valid_org/test-generation-config-org' , push_to_hub=a , use_auth_token=self._token ) lowerCAmelCase__ : List[str] = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) )
307
0
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 lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """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 A__ ( _snake_case , _snake_case ): lowercase = 'swin' lowercase = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : Any , a : List[str]=224 , a : Any=4 , a : Optional[int]=3 , a : List[str]=96 , a : Dict=[2, 2, 6, 2] , a : int=[3, 6, 12, 24] , a : str=7 , a : int=4.0 , a : List[str]=True , a : int=0.0 , a : List[Any]=0.0 , a : int=0.1 , a : Tuple="gelu" , a : Any=False , a : int=0.0_2 , a : List[Any]=1E-5 , a : Optional[int]=32 , a : Tuple=None , a : List[str]=None , **a : List[str] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) lowerCAmelCase__ : List[Any] = image_size lowerCAmelCase__ : List[Any] = patch_size lowerCAmelCase__ : Tuple = num_channels lowerCAmelCase__ : List[Any] = embed_dim lowerCAmelCase__ : Dict = depths lowerCAmelCase__ : List[str] = len(UpperCamelCase__ ) lowerCAmelCase__ : Optional[int] = num_heads lowerCAmelCase__ : str = window_size lowerCAmelCase__ : Optional[Any] = mlp_ratio lowerCAmelCase__ : str = qkv_bias lowerCAmelCase__ : Dict = hidden_dropout_prob lowerCAmelCase__ : int = attention_probs_dropout_prob lowerCAmelCase__ : int = drop_path_rate lowerCAmelCase__ : Dict = hidden_act lowerCAmelCase__ : Optional[Any] = use_absolute_embeddings lowerCAmelCase__ : Optional[int] = layer_norm_eps lowerCAmelCase__ : Optional[int] = initializer_range lowerCAmelCase__ : Union[str, 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 lowerCAmelCase__ : Tuple = int(embed_dim * 2 ** (len(UpperCamelCase__ ) - 1) ) lowerCAmelCase__ : Any = ['stem'] + [f'''stage{idx}''' for idx in range(1 , len(UpperCamelCase__ ) + 1 )] lowerCAmelCase__ , lowerCAmelCase__ : str = get_aligned_output_features_output_indices( out_features=UpperCamelCase__ , out_indices=UpperCamelCase__ , stage_names=self.stage_names ) class A__ ( _snake_case ): lowercase = version.parse('1.11' ) @property def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def _lowerCamelCase ( self : str ): '''simple docstring''' return 1E-4
363
import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import ( DiffusionPipeline, UnCLIPImageVariationPipeline, UnCLIPScheduler, UNetaDConditionModel, UNetaDModel, ) from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, load_image, require_torch_gpu, skip_mps from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = UnCLIPImageVariationPipeline lowercase = IMAGE_VARIATION_PARAMS - {'height', 'width', 'guidance_scale'} lowercase = IMAGE_VARIATION_BATCH_PARAMS lowercase = [ 'generator', 'return_dict', 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] lowercase = False @property def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' return 32 @property def _lowerCamelCase ( self : int ): '''simple docstring''' return self.time_input_dim @property def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return self.time_input_dim * 4 @property def _lowerCamelCase ( self : Any ): '''simple docstring''' return 100 @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) return tokenizer @property def _lowerCamelCase ( self : Any ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) return CLIPTextModelWithProjection(a ) @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : List[Any] = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) return CLIPVisionModelWithProjection(a ) @property def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Union[str, Any] = { 'clip_embeddings_dim': self.text_embedder_hidden_size, 'time_embed_dim': self.time_embed_dim, 'cross_attention_dim': self.cross_attention_dim, } lowerCAmelCase__ : Optional[Any] = UnCLIPTextProjModel(**a ) return model @property def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : str = { 'sample_size': 32, # RGB in channels 'in_channels': 3, # Out channels is double in channels because predicts mean and variance 'out_channels': 6, 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': 'identity', } lowerCAmelCase__ : str = UNetaDConditionModel(**a ) return model @property def _lowerCamelCase ( self : str ): '''simple docstring''' return { "sample_size": 64, "layers_per_block": 1, "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"), "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"), "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "in_channels": 6, "out_channels": 3, } @property def _lowerCamelCase ( self : str ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Any = UNetaDModel(**self.dummy_super_res_kwargs ) return model @property def _lowerCamelCase ( self : int ): '''simple docstring''' torch.manual_seed(1 ) lowerCAmelCase__ : List[str] = UNetaDModel(**self.dummy_super_res_kwargs ) return model def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = self.dummy_decoder lowerCAmelCase__ : Optional[int] = self.dummy_text_proj lowerCAmelCase__ : Any = self.dummy_text_encoder lowerCAmelCase__ : Any = self.dummy_tokenizer lowerCAmelCase__ : Any = self.dummy_super_res_first lowerCAmelCase__ : Optional[int] = self.dummy_super_res_last lowerCAmelCase__ : Dict = UnCLIPScheduler( variance_type='learned_range' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = UnCLIPScheduler( variance_type='fixed_small_log' , prediction_type='epsilon' , num_train_timesteps=1_000 , ) lowerCAmelCase__ : Any = CLIPImageProcessor(crop_size=32 , size=32 ) lowerCAmelCase__ : Optional[int] = self.dummy_image_encoder return { "decoder": decoder, "text_encoder": text_encoder, "tokenizer": tokenizer, "text_proj": text_proj, "feature_extractor": feature_extractor, "image_encoder": image_encoder, "super_res_first": super_res_first, "super_res_last": super_res_last, "decoder_scheduler": decoder_scheduler, "super_res_scheduler": super_res_scheduler, } def _lowerCamelCase ( self : Any , a : Dict , a : List[str]=0 , a : List[str]=True ): '''simple docstring''' lowerCAmelCase__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(a ) ).to(a ) if str(a ).startswith('mps' ): lowerCAmelCase__ : Optional[int] = torch.manual_seed(a ) else: lowerCAmelCase__ : str = torch.Generator(device=a ).manual_seed(a ) if pil_image: lowerCAmelCase__ : Optional[int] = input_image * 0.5 + 0.5 lowerCAmelCase__ : Dict = input_image.clamp(0 , 1 ) lowerCAmelCase__ : List[Any] = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() lowerCAmelCase__ : Union[str, Any] = DiffusionPipeline.numpy_to_pil(a )[0] return { "image": input_image, "generator": generator, "decoder_num_inference_steps": 2, "super_res_num_inference_steps": 2, "output_type": "np", } def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = 'cpu' lowerCAmelCase__ : Any = self.get_dummy_components() lowerCAmelCase__ : List[str] = self.pipeline_class(**a ) lowerCAmelCase__ : Dict = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : str = pipe(**a ) lowerCAmelCase__ : Optional[Any] = output.images lowerCAmelCase__ : str = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : List[str] = np.array( [ 0.9_9_9_7, 0.0_0_0_2, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_6_9, 0.0_0_2_3, 0.9_9_9_7, 0.9_9_6_9, 0.9_9_7_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 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = 'cpu' lowerCAmelCase__ : Dict = self.get_dummy_components() lowerCAmelCase__ : Optional[int] = self.pipeline_class(**a ) lowerCAmelCase__ : int = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = pipe(**a ) lowerCAmelCase__ : Union[str, Any] = output.images lowerCAmelCase__ : int = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : int = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Tuple = image[0, -3:, -3:, -1] lowerCAmelCase__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : str = np.array([0.9_9_9_7, 0.0_0_0_3, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_7_0, 0.0_0_2_4, 0.9_9_9_7, 0.9_9_7_1, 0.9_9_7_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Tuple = 'cpu' lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : Tuple = self.pipeline_class(**a ) lowerCAmelCase__ : Union[str, Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Tuple = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : List[str] = [ pipeline_inputs['image'], pipeline_inputs['image'], ] lowerCAmelCase__ : Optional[int] = pipe(**a ) lowerCAmelCase__ : Tuple = output.images lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Union[str, Any] = [ tuple_pipeline_inputs['image'], tuple_pipeline_inputs['image'], ] lowerCAmelCase__ : str = pipe( **a , return_dict=a , )[0] lowerCAmelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : Any = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (2, 64, 64, 3) lowerCAmelCase__ : Union[str, Any] = np.array( [ 0.9_9_9_7, 0.9_9_8_9, 0.0_0_0_8, 0.0_0_2_1, 0.9_9_6_0, 0.0_0_1_8, 0.0_0_1_4, 0.0_0_0_2, 0.9_9_3_3, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch.device('cpu' ) class A__ : lowercase = 1 lowerCAmelCase__ : Optional[Any] = self.get_dummy_components() lowerCAmelCase__ : Dict = self.pipeline_class(**a ) lowerCAmelCase__ : Optional[Any] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.Generator(device=a ).manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe.decoder.dtype lowerCAmelCase__ : Union[str, Any] = 1 lowerCAmelCase__ : str = ( batch_size, pipe.decoder.config.in_channels, pipe.decoder.config.sample_size, pipe.decoder.config.sample_size, ) lowerCAmelCase__ : List[Any] = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[str] = ( batch_size, pipe.super_res_first.config.in_channels // 2, pipe.super_res_first.config.sample_size, pipe.super_res_first.config.sample_size, ) lowerCAmelCase__ : Any = pipe.prepare_latents( a , dtype=a , device=a , generator=a , latents=a , scheduler=DummyScheduler() ) lowerCAmelCase__ : List[Any] = self.get_dummy_inputs(a , pil_image=a ) lowerCAmelCase__ : Optional[int] = pipe( **a , decoder_latents=a , super_res_latents=a ).images lowerCAmelCase__ : Optional[Any] = self.get_dummy_inputs(a , pil_image=a ) # Don't pass image, instead pass embedding lowerCAmelCase__ : Union[str, Any] = pipeline_inputs.pop('image' ) lowerCAmelCase__ : Union[str, Any] = pipe.image_encoder(a ).image_embeds lowerCAmelCase__ : List[Any] = pipe( **a , decoder_latents=a , super_res_latents=a , image_embeddings=a , ).images # make sure passing text embeddings manually is identical assert np.abs(img_out_a - img_out_a ).max() < 1E-4 @skip_mps def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device == 'cpu' # Check is relaxed because there is not a torch 2.0 sliced attention added kv processor lowerCAmelCase__ : int = 1E-2 self._test_attention_slicing_forward_pass( test_max_difference=a , expected_max_diff=a ) @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = torch_device == 'cpu' lowerCAmelCase__ : Any = True lowerCAmelCase__ : Optional[Any] = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] self._test_inference_batch_single_identical( test_max_difference=a , relax_max_difference=a , additional_params_copy_to_batched_inputs=a , ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Tuple = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] if torch_device == "mps": # TODO: MPS errors with larger batch sizes lowerCAmelCase__ : List[str] = [2, 3] self._test_inference_batch_consistent( batch_sizes=a , additional_params_copy_to_batched_inputs=a , ) else: self._test_inference_batch_consistent( additional_params_copy_to_batched_inputs=a ) @skip_mps def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent() @skip_mps def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_optional_components() @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Tuple ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Dict = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/unclip/cat.png' ) lowerCAmelCase__ : List[Any] = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/unclip/karlo_v1_alpha_cat_variation_fp16.npy' ) lowerCAmelCase__ : Tuple = UnCLIPImageVariationPipeline.from_pretrained( 'kakaobrain/karlo-v1-alpha-image-variations' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Union[str, Any] = pipeline.to(a ) pipeline.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Dict = torch.Generator(device='cpu' ).manual_seed(0 ) lowerCAmelCase__ : List[str] = pipeline( a , generator=a , output_type='np' , ) lowerCAmelCase__ : Union[str, Any] = output.images[0] assert image.shape == (256, 256, 3) assert_mean_pixel_difference(a , a , 15 )
307
0
import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if 'encoder.embeddings' not in key else 0 for key, param in state_dict.items() ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: lowerCAmelCase__ : Dict = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue lowerCAmelCase__ : List[Any] = key.replace('heads.cmd.mim_head.cls.predictions' , 'mmm_image_head' ) lowerCAmelCase__ : Union[str, Any] = key.replace('heads.cmd.mlm_head.cls.predictions' , 'mmm_text_head' ) lowerCAmelCase__ : Optional[int] = key.replace('heads.cmd.itm_head.cls' , 'itm_head' ) lowerCAmelCase__ : List[str] = key.replace('heads.cmd.itm_head.pooler' , 'itm_head.pooler' ) lowerCAmelCase__ : Optional[int] = key.replace('heads.cmd.clip_head.logit_scale' , 'flava.logit_scale' ) lowerCAmelCase__ : int = key.replace('heads.fairseq_mlm.cls.predictions' , 'mlm_head' ) lowerCAmelCase__ : Any = key.replace('heads.imagenet.mim_head.cls.predictions' , 'mim_head' ) lowerCAmelCase__ : Optional[Any] = key.replace('mm_text_projection' , 'flava.text_to_mm_projection' ) lowerCAmelCase__ : int = key.replace('mm_image_projection' , 'flava.image_to_mm_projection' ) lowerCAmelCase__ : Dict = key.replace('image_encoder.module' , 'flava.image_model' ) lowerCAmelCase__ : Optional[Any] = key.replace('text_encoder.module' , 'flava.text_model' ) lowerCAmelCase__ : Union[str, Any] = key.replace('mm_encoder.module.encoder.cls_token' , 'flava.multimodal_model.cls_token' ) lowerCAmelCase__ : Optional[int] = key.replace('mm_encoder.module' , 'flava.multimodal_model' ) lowerCAmelCase__ : List[str] = key.replace('text_projection' , 'flava.text_projection' ) lowerCAmelCase__ : Any = key.replace('image_projection' , 'flava.image_projection' ) lowerCAmelCase__ : Optional[Any] = value.float() for key, value in codebook_state_dict.items(): lowerCAmelCase__ : List[str] = value return upgrade @torch.no_grad() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> Any: if config_path is not None: lowerCAmelCase__ : int = FlavaConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) else: lowerCAmelCase__ : Optional[Any] = FlavaConfig() lowerCAmelCase__ : Tuple = FlavaForPreTraining(SCREAMING_SNAKE_CASE_ ).eval() lowerCAmelCase__ : Union[str, Any] = convert_dalle_checkpoint(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , save_checkpoint=SCREAMING_SNAKE_CASE_ ) if os.path.exists(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : List[Any] = torch.load(SCREAMING_SNAKE_CASE_ , map_location='cpu' ) else: lowerCAmelCase__ : List[str] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ , map_location='cpu' ) lowerCAmelCase__ : str = upgrade_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) hf_model.load_state_dict(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Dict = hf_model.state_dict() lowerCAmelCase__ : Union[str, Any] = count_parameters(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Union[str, Any] = count_parameters(SCREAMING_SNAKE_CASE_ ) + count_parameters(SCREAMING_SNAKE_CASE_ ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-3 ) hf_model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to flava checkpoint""") parser.add_argument("""--codebook_path""", default=None, type=str, help="""Path to flava codebook checkpoint""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") lowerCamelCase__ = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
364
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: stooge(SCREAMING_SNAKE_CASE_ , 0 , len(SCREAMING_SNAKE_CASE_ ) - 1 ) return arr def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowerCAmelCase__ , lowerCAmelCase__ : Tuple = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowerCAmelCase__ : Union[str, Any] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) # Recursively sort last 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , i + t , (SCREAMING_SNAKE_CASE_) ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) if __name__ == "__main__": lowerCamelCase__ = input("""Enter numbers separated by a comma:\n""").strip() lowerCamelCase__ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ = 1_000 ) -> int: return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1 ) ) if __name__ == "__main__": print(solution())
365
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) lowerCamelCase__ = { """configuration_speech_to_text""": ["""SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Speech2TextConfig"""], """processing_speech_to_text""": ["""Speech2TextProcessor"""], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextTokenizer"""] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""Speech2TextFeatureExtractor"""] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFSpeech2TextForConditionalGeneration""", """TFSpeech2TextModel""", """TFSpeech2TextPreTrainedModel""", ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """Speech2TextForConditionalGeneration""", """Speech2TextModel""", """Speech2TextPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowerCamelCase__ = 1E-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class A__ : def __init__( self : Union[str, Any] , a : str , a : Dict=16 , a : int=13 , a : Union[str, Any]=7 , a : Any=14 , a : Any=10 , a : str=19 , a : Any=5 , a : Tuple=4 , a : Any=True , a : int=16 , a : Optional[int]=2 , a : Optional[int]=4 , a : int=4 , a : List[str]="gelu" , a : List[str]=0.1 , a : Dict=0.1 , a : Optional[int]=[1, 2, 3, 4, 5] , a : List[str]=25 , a : Optional[int]=5 , ): '''simple docstring''' lowerCAmelCase__ : List[Any] = d_model lowerCAmelCase__ : Union[str, Any] = parent lowerCAmelCase__ : Union[str, Any] = batch_size lowerCAmelCase__ : List[Any] = prediction_length lowerCAmelCase__ : List[Any] = context_length lowerCAmelCase__ : Dict = cardinality lowerCAmelCase__ : str = num_time_features lowerCAmelCase__ : Any = lags_sequence lowerCAmelCase__ : int = embedding_dimension lowerCAmelCase__ : Any = is_training lowerCAmelCase__ : Tuple = hidden_size lowerCAmelCase__ : List[Any] = num_hidden_layers lowerCAmelCase__ : Dict = num_attention_heads lowerCAmelCase__ : Any = intermediate_size lowerCAmelCase__ : Tuple = hidden_act lowerCAmelCase__ : Dict = hidden_dropout_prob lowerCAmelCase__ : Union[str, Any] = attention_probs_dropout_prob lowerCAmelCase__ : List[str] = context_length lowerCAmelCase__ : Tuple = prediction_length + label_length lowerCAmelCase__ : Optional[Any] = label_length lowerCAmelCase__ : Tuple = moving_average lowerCAmelCase__ : Tuple = autocorrelation_factor def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return AutoformerConfig( d_model=self.d_model , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def _lowerCamelCase ( self : List[str] , a : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[str] = config.context_length + max(config.lags_sequence ) lowerCAmelCase__ : Any = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) lowerCAmelCase__ : Dict = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) lowerCAmelCase__ : int = floats_tensor([self.batch_size, _past_length] ) lowerCAmelCase__ : List[Any] = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs lowerCAmelCase__ : Optional[int] = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) lowerCAmelCase__ : Tuple = floats_tensor([self.batch_size, config.prediction_length] ) lowerCAmelCase__ : Optional[Any] = { 'past_values': past_values, 'static_categorical_features': static_categorical_features, 'past_time_features': past_time_features, 'past_observed_mask': past_observed_mask, 'future_time_features': future_time_features, 'future_values': future_values, } return inputs_dict def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.get_config() lowerCAmelCase__ : Union[str, Any] = self.prepare_autoformer_inputs_dict(a ) return config, inputs_dict def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = self.prepare_config_and_inputs() return config, inputs_dict def _lowerCamelCase ( self : str , a : Optional[Any] , a : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : int = AutoformerModel(config=a ).to(a ).eval() lowerCAmelCase__ : Optional[int] = model(**a ) lowerCAmelCase__ : Optional[int] = outputs.encoder_last_hidden_state lowerCAmelCase__ : List[Any] = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: lowerCAmelCase__ : Optional[int] = model.get_encoder() encoder.save_pretrained(a ) lowerCAmelCase__ : Tuple = AutoformerEncoder.from_pretrained(a ).to(a ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : Dict = model.create_network_inputs(**a ) lowerCAmelCase__ , lowerCAmelCase__ : Optional[int] = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) lowerCAmelCase__ : Tuple = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) lowerCAmelCase__ : List[Any] = encoder(inputs_embeds=a )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) lowerCAmelCase__ : Optional[Any] = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) lowerCAmelCase__ : List[str] = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) lowerCAmelCase__ : Union[str, Any] = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) lowerCAmelCase__ : int = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: lowerCAmelCase__ : Any = model.get_decoder() decoder.save_pretrained(a ) lowerCAmelCase__ : List[Any] = AutoformerDecoder.from_pretrained(a ).to(a ) lowerCAmelCase__ : Any = decoder( trend=a , inputs_embeds=a , encoder_hidden_states=a , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class A__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): lowercase = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () lowercase = (AutoformerForPrediction,) if is_torch_available() else () lowercase = {'feature-extraction': AutoformerModel} if is_torch_available() else {} lowercase = False lowercase = False lowercase = False lowercase = False lowercase = False lowercase = False def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : int = AutoformerModelTester(self ) lowerCAmelCase__ : Union[str, Any] = ConfigTester(self , config_class=a , has_text_modality=a ) def _lowerCamelCase ( self : Any ): '''simple docstring''' self.config_tester.run_common_tests() def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : int = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: lowerCAmelCase__ : Dict = model_class(a ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(a ) lowerCAmelCase__ , lowerCAmelCase__ : str = model_class.from_pretrained(a , output_loading_info=a ) self.assertEqual(info['missing_keys'] , [] ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*a ) @unittest.skip(reason='Model has no tokens embeddings' ) def _lowerCamelCase ( self : int ): '''simple docstring''' pass def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : str = inspect.signature(getattr(a , 'forward' ) ) # The main input is the name of the argument after `self` lowerCAmelCase__ : Optional[int] = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , a ) def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase__ : str = model_class(a ) lowerCAmelCase__ : Optional[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase__ : str = [*signature.parameters.keys()] lowerCAmelCase__ : List[str] = [ 'past_values', 'past_time_features', 'past_observed_mask', 'static_categorical_features', 'static_real_features', 'future_values', 'future_time_features', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('future_observed_mask' ) expected_arg_names.extend( [ 'decoder_attention_mask', 'head_mask', 'decoder_head_mask', 'cross_attn_head_mask', 'encoder_outputs', 'past_key_values', 'output_hidden_states', 'output_attentions', 'use_cache', 'return_dict', ] ) self.assertListEqual(arg_names[: len(a )] , a ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Optional[int] = True lowerCAmelCase__ : Union[str, Any] = getattr(self.model_tester , 'seq_length' , a ) lowerCAmelCase__ : Union[str, Any] = getattr(self.model_tester , 'decoder_seq_length' , a ) lowerCAmelCase__ : Union[str, Any] = getattr(self.model_tester , 'encoder_seq_length' , a ) lowerCAmelCase__ : Optional[int] = getattr(self.model_tester , 'd_model' , a ) lowerCAmelCase__ : Dict = getattr(self.model_tester , 'num_attention_heads' , a ) lowerCAmelCase__ : List[Any] = d_model // num_attention_heads for model_class in self.all_model_classes: lowerCAmelCase__ : Optional[Any] = True lowerCAmelCase__ : Dict = False lowerCAmelCase__ : Optional[Any] = True lowerCAmelCase__ : Union[str, Any] = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): lowerCAmelCase__ : List[str] = model(**self._prepare_for_class(a , a ) ) lowerCAmelCase__ : Tuple = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(a ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] lowerCAmelCase__ : Optional[int] = True lowerCAmelCase__ : Tuple = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): lowerCAmelCase__ : List[str] = model(**self._prepare_for_class(a , a ) ) lowerCAmelCase__ : List[str] = outputs.encoder_attentions self.assertEqual(len(a ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) lowerCAmelCase__ : Optional[Any] = len(a ) lowerCAmelCase__ : Dict = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(a , a ) # decoder attentions lowerCAmelCase__ : List[Any] = outputs.decoder_attentions self.assertIsInstance(a , (list, tuple) ) self.assertEqual(len(a ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions lowerCAmelCase__ : Optional[int] = outputs.cross_attentions self.assertIsInstance(a , (list, tuple) ) self.assertEqual(len(a ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine lowerCAmelCase__ : int = True lowerCAmelCase__ : Tuple = True lowerCAmelCase__ : Tuple = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): lowerCAmelCase__ : List[str] = model(**self._prepare_for_class(a , a ) ) self.assertEqual(out_len + 2 , len(a ) ) lowerCAmelCase__ : str = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(a ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def _lowerCamelCase ( self : Any ): '''simple docstring''' super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_="train-batch.pt" ) -> Tuple: lowerCAmelCase__ : Union[str, Any] = hf_hub_download(repo_id='hf-internal-testing/tourism-monthly-batch' , filename=lowerCAmelCase__ , repo_type='dataset' ) lowerCAmelCase__ : List[str] = torch.load(lowerCAmelCase__ , map_location=lowerCAmelCase__ ) return batch @require_torch @slow class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : int = AutoformerModel.from_pretrained('huggingface/autoformer-tourism-monthly' ).to(a ) lowerCAmelCase__ : int = prepare_batch() with torch.no_grad(): lowerCAmelCase__ : Union[str, Any] = model( past_values=batch['past_values'] , past_time_features=batch['past_time_features'] , past_observed_mask=batch['past_observed_mask'] , static_categorical_features=batch['static_categorical_features'] , future_values=batch['future_values'] , future_time_features=batch['future_time_features'] , )[0] lowerCAmelCase__ : int = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , a ) lowerCAmelCase__ : Union[str, Any] = torch.tensor( [[0.3_5_9_3, -1.3_3_9_8, 0.6_3_3_0], [0.2_2_7_9, 1.5_3_9_6, -0.1_7_9_2], [0.0_4_5_0, 1.3_2_2_5, -0.2_3_3_5]] , device=a ) self.assertTrue(torch.allclose(output[0, :3, :3] , a , atol=a ) ) def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly' ).to(a ) lowerCAmelCase__ : Optional[int] = prepare_batch('val-batch.pt' ) with torch.no_grad(): lowerCAmelCase__ : Union[str, Any] = model( past_values=batch['past_values'] , past_time_features=batch['past_time_features'] , past_observed_mask=batch['past_observed_mask'] , static_categorical_features=batch['static_categorical_features'] , ).encoder_last_hidden_state lowerCAmelCase__ : str = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , a ) lowerCAmelCase__ : Union[str, Any] = torch.tensor( [[-0.0_7_3_4, -0.9_0_3_6, 0.8_3_5_8], [4.7_1_8_6, 2.4_1_1_3, 1.9_5_8_1], [1.7_9_5_3, 2.3_5_5_8, 1.2_9_7_0]] , device=a ) self.assertTrue(torch.allclose(output[0, :3, :3] , a , atol=a ) ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly' ).to(a ) lowerCAmelCase__ : int = prepare_batch('val-batch.pt' ) with torch.no_grad(): lowerCAmelCase__ : Dict = model.generate( static_categorical_features=batch['static_categorical_features'] , past_time_features=batch['past_time_features'] , past_values=batch['past_values'] , future_time_features=batch['future_time_features'] , past_observed_mask=batch['past_observed_mask'] , ) lowerCAmelCase__ : str = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , a ) lowerCAmelCase__ : int = torch.tensor([3_130.6_763, 4_056.5_293, 7_053.0_786] , device=a ) lowerCAmelCase__ : Dict = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , a , rtol=1E-1 ) )
366
import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = { 'task_specific_params': { 'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4}, 'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4}, 'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6}, } } lowerCAmelCase__ : int = { 'task_specific_params.summarization.length_penalty': 1.0, 'task_specific_params.summarization.max_length': 128, 'task_specific_params.summarization.min_length': 12, 'task_specific_params.summarization.num_beams': 4, 'task_specific_params.summarization_cnn.length_penalty': 2.0, 'task_specific_params.summarization_cnn.max_length': 142, 'task_specific_params.summarization_cnn.min_length': 56, 'task_specific_params.summarization_cnn.num_beams': 4, 'task_specific_params.summarization_xsum.length_penalty': 1.0, 'task_specific_params.summarization_xsum.max_length': 62, 'task_specific_params.summarization_xsum.min_length': 11, 'task_specific_params.summarization_xsum.num_beams': 6, } self.assertEqual(flatten_dict(a ) , a ) def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(a ) , x.transpose() ) ) lowerCAmelCase__ : List[str] = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : int = torch.tensor(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(transpose(a ) , transpose(a ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Dict = tf.constant(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , transpose(a , axes=(1, 2, 0) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : int = jnp.array(a ) self.assertTrue(np.allclose(transpose(a ) , np.asarray(transpose(a ) ) ) ) lowerCAmelCase__ : Any = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = jnp.array(a ) self.assertTrue(np.allclose(transpose(a , axes=(1, 2, 0) ) , np.asarray(transpose(a , axes=(1, 2, 0) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Any = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.reshape(a , (4, 3) ) ) ) lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.reshape(a , (12, 5) ) ) ) @require_torch def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_tf def _lowerCamelCase ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[Any] = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , reshape(a , (4, 3) ).numpy() ) ) lowerCAmelCase__ : Dict = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , reshape(a , (12, 5) ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Dict = np.random.randn(3 , 4 ) lowerCAmelCase__ : List[str] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (4, 3) ) , np.asarray(reshape(a , (4, 3) ) ) ) ) lowerCAmelCase__ : str = np.random.randn(3 , 4 , 5 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(reshape(a , (12, 5) ) , np.asarray(reshape(a , (12, 5) ) ) ) ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(a ) , np.squeeze(a ) ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.squeeze(a , axis=2 ) ) ) @require_torch def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : Optional[Any] = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Dict = torch.tensor(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a ) , squeeze(a ).numpy() ) ) lowerCAmelCase__ : int = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : str = tf.constant(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , squeeze(a , axis=2 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : List[str] = np.random.randn(1 , 3 , 4 ) lowerCAmelCase__ : Union[str, Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a ) , np.asarray(squeeze(a ) ) ) ) lowerCAmelCase__ : str = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase__ : Optional[Any] = jnp.array(a ) self.assertTrue(np.allclose(squeeze(a , axis=2 ) , np.asarray(squeeze(a , axis=2 ) ) ) ) def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.expand_dims(a , axis=1 ) ) ) @require_torch def _lowerCamelCase ( self : Dict ): '''simple docstring''' lowerCAmelCase__ : str = np.random.randn(3 , 4 ) lowerCAmelCase__ : str = torch.tensor(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_tf def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = np.random.randn(3 , 4 ) lowerCAmelCase__ : Any = tf.constant(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , expand_dims(a , axis=1 ).numpy() ) ) @require_flax def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : int = np.random.randn(3 , 4 ) lowerCAmelCase__ : Tuple = jnp.array(a ) self.assertTrue(np.allclose(expand_dims(a , axis=1 ) , np.asarray(expand_dims(a , axis=1 ) ) ) )
307
0
import logging from transformers import PretrainedConfig lowerCamelCase__ = logging.getLogger(__name__) lowerCamelCase__ = { """bertabs-finetuned-cnndm""": """https://huggingface.co/remi/bertabs-finetuned-cnndm-extractive-abstractive-summarization/resolve/main/config.json""", } class A__ ( UpperCAmelCase_ ): lowercase = """bertabs""" def __init__( self : Any , a : str=30_522 , a : Optional[int]=512 , a : str=6 , a : str=512 , a : List[str]=8 , a : Tuple=512 , a : Optional[Any]=0.2 , a : Any=6 , a : Any=768 , a : Optional[int]=8 , a : str=2_048 , a : List[Any]=0.2 , **a : Optional[int] , ): '''simple docstring''' super().__init__(**__lowercase ) lowerCAmelCase__ : int = vocab_size lowerCAmelCase__ : List[Any] = max_pos lowerCAmelCase__ : List[Any] = enc_layers lowerCAmelCase__ : Optional[int] = enc_hidden_size lowerCAmelCase__ : Dict = enc_heads lowerCAmelCase__ : int = enc_ff_size lowerCAmelCase__ : Tuple = enc_dropout lowerCAmelCase__ : List[Any] = dec_layers lowerCAmelCase__ : Tuple = dec_hidden_size lowerCAmelCase__ : Tuple = dec_heads lowerCAmelCase__ : Optional[Any] = dec_ff_size lowerCAmelCase__ : Optional[int] = dec_dropout
367
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position lowerCamelCase__ = """2.13.1""" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("""3.7"""): raise ImportWarning( """To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition.""" ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( """To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n""" """If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.""" ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip lowerCamelCase__ = concatenate_datasets lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadManager lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadConfig lowerCamelCase__ = DownloadMode lowerCamelCase__ = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
307
0
import warnings from ...utils import logging from .image_processing_deformable_detr import DeformableDetrImageProcessor lowerCamelCase__ = logging.get_logger(__name__) class A__ ( UpperCamelCase__ ): def __init__( self : str , *a : List[Any] , **a : Optional[Any] ): '''simple docstring''' warnings.warn( 'The class DeformableDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use DeformableDetrImageProcessor instead.' , __a , ) super().__init__(*__a , **__a )
368
import gc import unittest import numpy as np import torch from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( __magic_name__ , unittest.TestCase ): lowercase = DanceDiffusionPipeline lowercase = UNCONDITIONAL_AUDIO_GENERATION_PARAMS lowercase = PipelineTesterMixin.required_optional_params - { 'callback', 'latents', 'callback_steps', 'output_type', 'num_images_per_prompt', } lowercase = UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS lowercase = False lowercase = False def _lowerCamelCase ( self : List[str] ): '''simple docstring''' torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = UNetaDModel( block_out_channels=(32, 32, 64) , extra_in_channels=16 , sample_size=512 , sample_rate=16_000 , in_channels=2 , out_channels=2 , flip_sin_to_cos=a , use_timestep_embedding=a , time_embedding_type='fourier' , mid_block_type='UNetMidBlock1D' , down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D') , up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip') , ) lowerCAmelCase__ : Tuple = IPNDMScheduler() lowerCAmelCase__ : str = { 'unet': unet, 'scheduler': scheduler, } return components def _lowerCamelCase ( self : int , a : Dict , a : List[str]=0 ): '''simple docstring''' if str(a ).startswith('mps' ): lowerCAmelCase__ : Union[str, Any] = torch.manual_seed(a ) else: lowerCAmelCase__ : Optional[Any] = torch.Generator(device=a ).manual_seed(a ) lowerCAmelCase__ : Optional[Any] = { 'batch_size': 1, 'generator': generator, 'num_inference_steps': 4, } return inputs def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'cpu' # ensure determinism for the device-dependent torch.Generator lowerCAmelCase__ : int = self.get_dummy_components() lowerCAmelCase__ : List[str] = DanceDiffusionPipeline(**a ) lowerCAmelCase__ : Any = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : List[str] = self.get_dummy_inputs(a ) lowerCAmelCase__ : List[Any] = pipe(**a ) lowerCAmelCase__ : List[str] = output.audios lowerCAmelCase__ : Optional[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, components["unet"].sample_size) lowerCAmelCase__ : List[Any] = np.array([-0.7_2_6_5, 1.0_0_0_0, -0.8_3_8_8, 0.1_1_7_5, 0.9_4_9_8, -1.0_0_0_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def _lowerCamelCase ( self : str ): '''simple docstring''' return super().test_save_load_local() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) @skip_mps def _lowerCamelCase ( self : List[str] ): '''simple docstring''' return super().test_save_load_optional_components() @skip_mps def _lowerCamelCase ( self : Tuple ): '''simple docstring''' return super().test_attention_slicing_forward_pass() def _lowerCamelCase ( self : List[str] ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class A__ ( unittest.TestCase ): def _lowerCamelCase ( self : Dict ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' lowerCAmelCase__ : Tuple = torch_device lowerCAmelCase__ : List[str] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' ) lowerCAmelCase__ : List[str] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : Optional[int] = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : int = output.audios lowerCAmelCase__ : List[Any] = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : Dict = np.array([-0.0_1_9_2, -0.0_2_3_1, -0.0_3_1_8, -0.0_0_5_9, 0.0_0_0_2, -0.0_0_2_0] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 def _lowerCamelCase ( self : Tuple ): '''simple docstring''' lowerCAmelCase__ : str = torch_device lowerCAmelCase__ : List[Any] = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' , torch_dtype=torch.floataa ) lowerCAmelCase__ : Optional[int] = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowerCAmelCase__ : str = torch.manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipe(generator=a , num_inference_steps=100 , audio_length_in_s=4.0_9_6 ) lowerCAmelCase__ : str = output.audios lowerCAmelCase__ : Tuple = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) lowerCAmelCase__ : int = np.array([-0.0_3_6_7, -0.0_4_8_8, -0.0_7_7_1, -0.0_5_2_5, -0.0_4_4_4, -0.0_3_4_1] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2
307
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : list[list[str]] = [[] for _ in range(lowerCAmelCase_ )] lowerCAmelCase__ : Optional[int] = key - 1 if key <= 0: raise ValueError('Height of grid can\'t be 0 or negative' ) if key == 1 or len(lowerCAmelCase_ ) <= key: return input_string for position, character in enumerate(lowerCAmelCase_ ): lowerCAmelCase__ : List[Any] = position % (lowest * 2) # puts it in bounds lowerCAmelCase__ : Optional[int] = min(lowerCAmelCase_ , lowest * 2 - num ) # creates zigzag pattern temp_grid[num].append(lowerCAmelCase_ ) lowerCAmelCase__ : Optional[Any] = [''.join(lowerCAmelCase_ ) for row in temp_grid] lowerCAmelCase__ : Any = ''.join(lowerCAmelCase_ ) return output_string def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : Any = [] lowerCAmelCase__ : int = key - 1 if key <= 0: raise ValueError('Height of grid can\'t be 0 or negative' ) if key == 1: return input_string lowerCAmelCase__ : list[list[str]] = [[] for _ in range(lowerCAmelCase_ )] # generates template for position in range(len(lowerCAmelCase_ ) ): lowerCAmelCase__ : Union[str, Any] = position % (lowest * 2) # puts it in bounds lowerCAmelCase__ : Any = min(lowerCAmelCase_ , lowest * 2 - num ) # creates zigzag pattern temp_grid[num].append('*' ) lowerCAmelCase__ : Dict = 0 for row in temp_grid: # fills in the characters lowerCAmelCase__ : Optional[Any] = input_string[counter : counter + len(lowerCAmelCase_ )] grid.append(list(lowerCAmelCase_ ) ) counter += len(lowerCAmelCase_ ) lowerCAmelCase__ : Optional[int] = '' # reads as zigzag for position in range(len(lowerCAmelCase_ ) ): lowerCAmelCase__ : str = position % (lowest * 2) # puts it in bounds lowerCAmelCase__ : str = min(lowerCAmelCase_ , lowest * 2 - num ) # creates zigzag pattern output_string += grid[num][0] grid[num].pop(0 ) return output_string def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> dict[int, str]: lowerCAmelCase__ : Any = {} for key_guess in range(1 , len(lowerCAmelCase_ ) ): # tries every key lowerCAmelCase__ : Optional[int] = decrypt(lowerCAmelCase_ , lowerCAmelCase_ ) return results if __name__ == "__main__": import doctest doctest.testmod()
369
from ..utils import DummyObject, requires_backends class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Union[str, Any] , *a : Optional[int] , **a : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : List[Any] , **a : int ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Any , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Optional[Any] , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Optional[int] , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : List[Any] , *a : List[str] , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : Union[str, Any] , **a : Optional[int] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : List[Any] , *a : Dict , **a : List[str] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Dict , **a : List[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : List[str] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : Dict , *a : str , **a : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : Any , **a : Any ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Any , *a : List[Any] , **a : str ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'transformers', 'onnx'] def __init__( self : str , *a : Union[str, Any] , **a : Optional[Any] ): '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : int , *a : Union[str, Any] , **a : Dict ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def _lowerCamelCase ( cls : Optional[int] , *a : Tuple , **a : List[str] ): '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] )
307
0