(
profiler: Callable,
func_code: str,
entry_point: str,
test_inputs: List[Any],
timeout_second_per_test: float,
memory_bound_gb: int,
warmup_inputs: Optional[List[Any]],
# shared memory
compute_cost, # Value("d", 0.0),
progress, # Value("i", 0),
)
| 68 | |
| 69 | |
| 70 | def get_instruction_count_shared_mem( |
| 71 | profiler: Callable, |
| 72 | func_code: str, |
| 73 | entry_point: str, |
| 74 | test_inputs: List[Any], |
| 75 | timeout_second_per_test: float, |
| 76 | memory_bound_gb: int, |
| 77 | warmup_inputs: Optional[List[Any]], |
| 78 | # shared memory |
| 79 | compute_cost, # Value("d", 0.0), |
| 80 | progress, # Value("i", 0), |
| 81 | ) -> Optional[float]: |
| 82 | |
| 83 | error = None |
| 84 | |
| 85 | with create_tempdir(): |
| 86 | # These system calls are needed when cleaning up tempdir. |
| 87 | import os |
| 88 | import shutil |
| 89 | |
| 90 | rmtree = shutil.rmtree |
| 91 | rmdir = os.rmdir |
| 92 | chdir = os.chdir |
| 93 | |
| 94 | # Disable functionalities that can make destructive changes to the test. |
| 95 | maximum_memory_bytes = memory_bound_gb * 1024 * 1024 * 1024 |
| 96 | reliability_guard(maximum_memory_bytes=maximum_memory_bytes) |
| 97 | exec_globals = {} |
| 98 | |
| 99 | # run (eval) the func def |
| 100 | exec(func_code, exec_globals) |
| 101 | fn = exec_globals[entry_point] |
| 102 | |
| 103 | # warmup the function |
| 104 | if warmup_inputs: |
| 105 | for _ in range(3): |
| 106 | fn(*warmup_inputs) |
| 107 | |
| 108 | progress.value = _STAT_START |
| 109 | try: # run the function |
| 110 | with time_limit(timeout_second_per_test): |
| 111 | with swallow_io(): |
| 112 | compute_cost.value = profiler(fn, test_inputs) |
| 113 | progress.value = _STAT_SUCC |
| 114 | except TimeoutException: |
| 115 | print("[Warning] Profiling hits TimeoutException") |
| 116 | except MemoryError: |
| 117 | print("[Warning] Profiling hits MemoryError") |
| 118 | except: |
| 119 | print("[CRITICAL] ! Unknown exception during profiling !") |
| 120 | error = format_exc() |
| 121 | print(error) |
| 122 | |
| 123 | if progress.value != _STAT_SUCC: |
| 124 | progress.value = _STAT_ERROR |
| 125 | |
| 126 | # Needed for cleaning up. |
| 127 | shutil.rmtree = rmtree |
nothing calls this directly
no test coverage detected