To run a test in a subprocess. In particular, this can avoid (GPU) memory issue. Args: test_case (`unittest.TestCase`): The test that will run `target_func`. target_func (`Callable`): The function implementing the actual testing logic. inputs
(test_case, target_func, inputs=None, timeout=None)
| 1076 | |
| 1077 | # Taken from: https://github.com/huggingface/transformers/blob/3658488ff77ff8d45101293e749263acf437f4d5/src/transformers/testing_utils.py#L1787 |
| 1078 | def run_test_in_subprocess(test_case, target_func, inputs=None, timeout=None): |
| 1079 | """ |
| 1080 | To run a test in a subprocess. In particular, this can avoid (GPU) memory issue. |
| 1081 | |
| 1082 | Args: |
| 1083 | test_case (`unittest.TestCase`): |
| 1084 | The test that will run `target_func`. |
| 1085 | target_func (`Callable`): |
| 1086 | The function implementing the actual testing logic. |
| 1087 | inputs (`dict`, *optional*, defaults to `None`): |
| 1088 | The inputs that will be passed to `target_func` through an (input) queue. |
| 1089 | timeout (`int`, *optional*, defaults to `None`): |
| 1090 | The timeout (in seconds) that will be passed to the input and output queues. If not specified, the env. |
| 1091 | variable `PYTEST_TIMEOUT` will be checked. If still `None`, its value will be set to `600`. |
| 1092 | """ |
| 1093 | if timeout is None: |
| 1094 | timeout = int(os.environ.get("PYTEST_TIMEOUT", 600)) |
| 1095 | |
| 1096 | start_methohd = "spawn" |
| 1097 | ctx = multiprocessing.get_context(start_methohd) |
| 1098 | |
| 1099 | input_queue = ctx.Queue(1) |
| 1100 | output_queue = ctx.JoinableQueue(1) |
| 1101 | |
| 1102 | # We can't send `unittest.TestCase` to the child, otherwise we get issues regarding pickle. |
| 1103 | input_queue.put(inputs, timeout=timeout) |
| 1104 | |
| 1105 | process = ctx.Process(target=target_func, args=(input_queue, output_queue, timeout)) |
| 1106 | process.start() |
| 1107 | # Kill the child process if we can't get outputs from it in time: otherwise, the hanging subprocess prevents |
| 1108 | # the test to exit properly. |
| 1109 | try: |
| 1110 | results = output_queue.get(timeout=timeout) |
| 1111 | output_queue.task_done() |
| 1112 | except Exception as e: |
| 1113 | process.terminate() |
| 1114 | test_case.fail(e) |
| 1115 | process.join(timeout=timeout) |
| 1116 | |
| 1117 | if results["error"] is not None: |
| 1118 | test_case.fail(f"{results['error']}") |
| 1119 | |
| 1120 | |
| 1121 | class CaptureLogger: |
nothing calls this directly
no test coverage detected
searching dependent graphs…