



import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append('''.''')






def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[Any]	)  ->						Union[str, Any]:
					__lowerCAmelCase	   =test_file.split(os.path.sep	)
					if components[0:2] != ["tests", "models"]:
										raise ValueError(
										    """`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got """
										    f"""{test_file} instead."""	)
					__lowerCAmelCase	   =components[-1]
					if not test_fn.endswith("""py"""	):
										raise ValueError(f"""`test_file` should be a python file. Got {test_fn} instead."""	)
					if not test_fn.startswith("""test_modeling_"""	):
										raise ValueError(
										    f"""`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead."""	)

					__lowerCAmelCase	   =components[:-1] + [test_fn.replace(""".py"""				,   """"""	)]
					__lowerCAmelCase	   =""".""".join(__lowerCamelCase	)

					return test_module_path






def 					__lowerCAmelCase		(  __lowerCamelCase	:    int	)  ->						List[str]:
					__lowerCAmelCase	   =get_module_path(__lowerCamelCase	)
					__lowerCAmelCase	   =importlib.import_module(__lowerCamelCase	)

					return test_module






def 					__lowerCAmelCase		(  __lowerCamelCase	:    str	)  ->						Optional[int]:
					__lowerCAmelCase	   =[]
					__lowerCAmelCase	   =get_test_module(__lowerCamelCase	)
					for attr in dir(__lowerCamelCase	):
										if attr.endswith("""ModelTester"""	):
															tester_classes.append(getattr(__lowerCamelCase				,   __lowerCamelCase	)	)

    # sort with class names
					return sorted(__lowerCamelCase				,   key=lambda __lowerCamelCase	: x.__name__	)






def 					__lowerCAmelCase		(  __lowerCamelCase	:    Tuple	)  ->						Optional[int]:
					__lowerCAmelCase	   =[]
					__lowerCAmelCase	   =get_test_module(__lowerCamelCase	)
					for attr in dir(__lowerCamelCase	):
										__lowerCAmelCase	   =getattr(__lowerCamelCase				,   __lowerCamelCase	)
										# (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
										# `all_model_classes` is not empty (which also excludes other special classes).
										__lowerCAmelCase	   =getattr(__lowerCamelCase				,   """all_model_classes"""				,   []	)
										if len(__lowerCamelCase	) > 0:
															test_classes.append(__lowerCamelCase	)

    # sort with class names
					return sorted(__lowerCamelCase				,   key=lambda __lowerCamelCase	: x.__name__	)






def 					__lowerCAmelCase		(  __lowerCamelCase	:    Any	)  ->						List[Any]:
					__lowerCAmelCase	   =get_test_classes(__lowerCamelCase	)
					__lowerCAmelCase	   =set()
					for test_class in test_classes:
										model_classes.update(test_class.all_model_classes	)

					# sort with class names
					return sorted(__lowerCamelCase				,   key=lambda __lowerCamelCase	: x.__name__	)






def 					__lowerCAmelCase		(  __lowerCamelCase	:    int	)  ->						List[Any]:
					__lowerCAmelCase	   =test_class()
					if hasattr(__lowerCamelCase				,   """setUp"""	):
										test.setUp()

					__lowerCAmelCase	   =None
					if hasattr(__lowerCamelCase				,   """model_tester"""	):
										# `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
										if test.model_tester is not None:
															__lowerCAmelCase	   =test.model_tester.__class__

					return model_tester






def 					__lowerCAmelCase		(  __lowerCamelCase	:    Union[str, Any]				,   __lowerCamelCase	:    Any	)  ->						Dict:
					__lowerCAmelCase	   =get_test_classes(__lowerCamelCase	)

					__lowerCAmelCase	   =[]
					for test_class in test_classes:
										if model_class in test_class.all_model_classes:
															target_test_classes.append(__lowerCamelCase	)

    # sort with class names
					return sorted(__lowerCamelCase				,   key=lambda __lowerCamelCase	: x.__name__	)






def 					__lowerCAmelCase		(  __lowerCamelCase	:    Union[str, Any]				,   __lowerCamelCase	:    int	)  ->						Optional[Any]:
					__lowerCAmelCase	   =get_test_classes_for_model(__lowerCamelCase				,   __lowerCamelCase	)

					__lowerCAmelCase	   =[]
					for test_class in test_classes:
										__lowerCAmelCase	   =get_model_tester_from_test_class(__lowerCamelCase	)
										if tester_class is not None:
															tester_classes.append(__lowerCamelCase	)

    # sort with class names
					return sorted(__lowerCamelCase				,   key=lambda __lowerCamelCase	: x.__name__	)






def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[str]	)  ->						Optional[Any]:
					__lowerCAmelCase	   =get_test_classes(__lowerCamelCase	)
					__lowerCAmelCase	   ={test_class: get_model_tester_from_test_class(__lowerCamelCase	) for test_class in test_classes}
					return test_tester_mapping






def 					__lowerCAmelCase		(  __lowerCamelCase	:    str	)  ->						List[Any]:
					__lowerCAmelCase	   =get_model_classes(__lowerCamelCase	)
					__lowerCAmelCase	   ={
					    model_class: get_test_classes_for_model(__lowerCamelCase				,   __lowerCamelCase	) for model_class in model_classes
					}
					return model_test_mapping






def 					__lowerCAmelCase		(  __lowerCamelCase	:    Any	)  ->						Any:
					__lowerCAmelCase	   =get_model_classes(__lowerCamelCase	)
					__lowerCAmelCase	   ={
					    model_class: get_tester_classes_for_model(__lowerCamelCase				,   __lowerCamelCase	) for model_class in model_classes
					}
					return model_to_tester_mapping






def 					__lowerCAmelCase		(  __lowerCamelCase	:    Optional[int]	)  ->						Any:
					if isinstance(__lowerCamelCase				,   __lowerCamelCase	):
										return o
					elif isinstance(__lowerCamelCase				,   __lowerCamelCase	):
										return o.__name__
					elif isinstance(__lowerCamelCase				,   (list, tuple)	):
										return [to_json(__lowerCamelCase	) for x in o]
					elif isinstance(__lowerCamelCase				,   __lowerCamelCase	):
										return {to_json(__lowerCamelCase	): to_json(__lowerCamelCase	) for k, v in o.items()}
					else:
										return o









def 					__lowerCAmelCase		(  __lowerCamelCase	:    int	)  ->						None:
					__lowerCAmelCase	   =generate_pascal_triangle(__lowerCamelCase	)
					for row_idx in range(__lowerCamelCase	):
										# Print left spaces
										for _ in range(num_rows - row_idx - 1	):
															print(end=""" """	)
										# Print row values
										for col_idx in range(row_idx + 1	):
															if col_idx != row_idx:
																				print(triangle[row_idx][col_idx]				,   end=""" """	)
															else:
																				print(triangle[row_idx][col_idx]				,   end=""""""	)
										print()






def 					__lowerCAmelCase		(  __lowerCamelCase	:    int	)  ->						list[list[int]]:

					if not isinstance(__lowerCamelCase				,   __lowerCamelCase	):
										raise TypeError("""The input value of 'num_rows' should be 'int'"""	)

					if num_rows == 0:
										return []
					elif num_rows < 0:
										raise ValueError(
										    """The input value of 'num_rows' should be greater than or equal to 0"""	)

					__lowerCAmelCase	   =[]
					for current_row_idx in range(__lowerCamelCase	):
										__lowerCAmelCase	   =populate_current_row(__lowerCamelCase				,   __lowerCamelCase	)
										triangle.append(__lowerCamelCase	)
					return triangle






def 					__lowerCAmelCase		(  __lowerCamelCase	:    list[list[int]]				,   __lowerCamelCase	:    int	)  ->						list[int]:
					__lowerCAmelCase	   =[-1] * (current_row_idx + 1)
					# first and last elements of current row are equal to 1
					__lowerCAmelCase   ,	__lowerCAmelCase	   =1, 1
					for current_col_idx in range(1				,   __lowerCamelCase	):
										calculate_current_element(
										    __lowerCamelCase				,   __lowerCamelCase				,   __lowerCamelCase				,   __lowerCamelCase	)
					return current_row






def 					__lowerCAmelCase		(  __lowerCamelCase	:    list[list[int]]				,   __lowerCamelCase	:    list[int]				,   __lowerCamelCase	:    int				,   __lowerCamelCase	:    int				,   )  ->						None:
					__lowerCAmelCase	   =triangle[current_row_idx - 1][current_col_idx - 1]
					__lowerCAmelCase	   =triangle[current_row_idx - 1][current_col_idx]
					__lowerCAmelCase	   =above_to_left_elt + above_to_right_elt






def 					__lowerCAmelCase		(  __lowerCamelCase	:    int	)  ->						list[list[int]]:

					if not isinstance(__lowerCamelCase				,   __lowerCamelCase	):
										raise TypeError("""The input value of 'num_rows' should be 'int'"""	)

					if num_rows == 0:
										return []
					elif num_rows < 0:
										raise ValueError(
										    """The input value of 'num_rows' should be greater than or equal to 0"""	)

					__lowerCAmelCase	   =[[1]]

					for row_index in range(1				,   __lowerCamelCase	):
										__lowerCAmelCase	   =[0] + result[-1] + [0]
										__lowerCAmelCase	   =row_index + 1
										# Calculate the number of distinct elements in a row
										__lowerCAmelCase	   =sum(divmod(__lowerCamelCase				,   2	)	)
										__lowerCAmelCase	   =[
										    temp_row[i - 1] + temp_row[i] for i in range(1				,   distinct_elements + 1	)
										]
										__lowerCAmelCase	   =row_first_half[: (row_index + 1) // 2]
										row_second_half.reverse()
										__lowerCAmelCase	   =row_first_half + row_second_half
										result.append(__lowerCamelCase	)

					return result






def 					__lowerCAmelCase		(  )  ->						None:
					from collections.abc import Callable
					from timeit import timeit

					def benchmark_a_function(__lowerCamelCase	:    Callable				,   __lowerCamelCase	:    int	) -> None:
										__lowerCAmelCase	   =f"""{func.__name__}({value})"""
										__lowerCAmelCase	   =timeit(f"""__main__.{call}"""				,   setup="""import __main__"""	)
										# print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
										print(f"""{call:38} -- {timing:.4f} seconds"""	)

					for value in range(15	):  # (1, 7, 14):
										for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
															benchmark_a_function(__lowerCamelCase				,   __lowerCamelCase	)
										print()


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				benchmark()




