Skip to content

Commit bd79e71

Browse files
Print consolidated log file for pytorch unit test automation scripts (#1433)
* Print consolidated log file for pytorch uts * Update run_entire_tests subprocess call as well * lint * Add ERROR string
1 parent d6fffe7 commit bd79e71

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

.automation_scripts/run_pytorch_unit_tests.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import os
2828
import shutil
2929
import subprocess
30+
from subprocess import STDOUT, CalledProcessError
3031

3132
from collections import namedtuple
3233
from datetime import datetime
@@ -63,6 +64,8 @@
6364
"distributed/test_distributed_spawn"
6465
]
6566

67+
CONSOLIDATED_LOG_FILE_NAME="pytorch_unit_tests.log"
68+
6669
def parse_xml_reports_as_dict(workflow_run_id, workflow_run_attempt, tag, workflow_name, path="."):
6770
test_cases = {}
6871
items_list = os.listdir(path)
@@ -198,6 +201,14 @@ def summarize_xml_files(path, workflow_name):
198201

199202
return res
200203

204+
def run_command_and_capture_output(cmd):
205+
try:
206+
print(f"Running command '{cmd}'")
207+
with open(CONSOLIDATED_LOG_FILE_PATH, "a+") as output_file:
208+
p = subprocess.run(cmd, shell=True, stdout=output_file, stderr=STDOUT, text=True)
209+
except CalledProcessError as e:
210+
print(f"ERROR: Cmd {cmd} failed with return code: {e.returncode}!")
211+
201212
def run_entire_tests(workflow_name, test_shell_path, overall_logs_path_current_run, test_reports_src):
202213
if os.path.exists(test_reports_src):
203214
shutil.rmtree(test_reports_src)
@@ -214,7 +225,7 @@ def run_entire_tests(workflow_name, test_shell_path, overall_logs_path_current_r
214225
os.environ['TEST_CONFIG'] = 'inductor'
215226
copied_logs_path = overall_logs_path_current_run + "inductor_xml_results_entire_tests/"
216227
# use test.sh for tests execution
217-
subprocess.call(test_shell_path, shell=True)
228+
run_command_and_capture_output(test_shell_path)
218229
copied_logs_path_destination = shutil.copytree(test_reports_src, copied_logs_path)
219230
entire_results_dict = summarize_xml_files(copied_logs_path_destination, workflow_name)
220231
return entire_results_dict
@@ -232,7 +243,7 @@ def run_priority_tests(workflow_name, test_run_test_path, overall_logs_path_curr
232243
# use run_test.py for tests execution
233244
default_priority_test_suites = " ".join(DEFAULT_CORE_TESTS)
234245
command = "python " + test_run_test_path + " --include " + default_priority_test_suites + " --exclude-jit-executor --exclude-distributed-tests --verbose"
235-
subprocess.call(command, shell=True)
246+
run_command_and_capture_output(command)
236247
del os.environ['HIP_VISIBLE_DEVICES']
237248
elif workflow_name == "distributed":
238249
os.environ['TEST_CONFIG'] = 'distributed'
@@ -241,7 +252,7 @@ def run_priority_tests(workflow_name, test_run_test_path, overall_logs_path_curr
241252
# use run_test.py for tests execution
242253
distributed_priority_test_suites = " ".join(DISTRIBUTED_CORE_TESTS)
243254
command = "python " + test_run_test_path + " --include " + distributed_priority_test_suites + " --distributed-tests --verbose"
244-
subprocess.call(command, shell=True)
255+
run_command_and_capture_output(command)
245256
del os.environ['HIP_VISIBLE_DEVICES']
246257
copied_logs_path_destination = shutil.copytree(test_reports_src, copied_logs_path)
247258
priority_results_dict = summarize_xml_files(copied_logs_path_destination, workflow_name)
@@ -261,7 +272,7 @@ def run_selected_tests(workflow_name, test_run_test_path, overall_logs_path_curr
261272
# use run_test.py for tests execution
262273
default_selected_test_suites = " ".join(selected_list)
263274
command = "python " + test_run_test_path + " --include " + default_selected_test_suites + " --exclude-jit-executor --exclude-distributed-tests --verbose"
264-
subprocess.call(command, shell=True)
275+
run_command_and_capture_output(command)
265276
del os.environ['HIP_VISIBLE_DEVICES']
266277
elif workflow_name == "distributed":
267278
os.environ['TEST_CONFIG'] = 'distributed'
@@ -270,7 +281,7 @@ def run_selected_tests(workflow_name, test_run_test_path, overall_logs_path_curr
270281
# use run_test.py for tests execution
271282
distributed_selected_test_suites = " ".join(selected_list)
272283
command = "python " + test_run_test_path + " --include " + distributed_selected_test_suites + " --distributed-tests --verbose"
273-
subprocess.call(command, shell=True)
284+
run_command_and_capture_output(command)
274285
del os.environ['HIP_VISIBLE_DEVICES']
275286
elif workflow_name == "inductor":
276287
os.environ['TEST_CONFIG'] = 'inductor'
@@ -287,11 +298,11 @@ def run_selected_tests(workflow_name, test_run_test_path, overall_logs_path_curr
287298
if inductor_selected_test_suites != "":
288299
inductor_selected_test_suites = inductor_selected_test_suites[:-1]
289300
command = "python " + test_run_test_path + " --include " + inductor_selected_test_suites + " --verbose"
290-
subprocess.call(command, shell=True)
301+
run_command_and_capture_output(command)
291302
if non_inductor_selected_test_suites != "":
292303
non_inductor_selected_test_suites = non_inductor_selected_test_suites[:-1]
293304
command = "python " + test_run_test_path + " --inductor --include " + non_inductor_selected_test_suites + " --verbose"
294-
subprocess.call(command, shell=True)
305+
run_command_and_capture_output(command)
295306
copied_logs_path_destination = shutil.copytree(test_reports_src, copied_logs_path)
296307
selected_results_dict = summarize_xml_files(copied_logs_path_destination, workflow_name)
297308

@@ -336,6 +347,11 @@ def run_test_and_summarize_results(
336347
# performed as Job ID
337348
str_current_datetime = str(current_datetime)
338349
overall_logs_path_current_run = repo_test_log_folder_path + str_current_datetime + "/"
350+
os.mkdir(overall_logs_path_current_run)
351+
352+
global CONSOLIDATED_LOG_FILE_PATH
353+
CONSOLIDATED_LOG_FILE_PATH = overall_logs_path_current_run + CONSOLIDATED_LOG_FILE_NAME
354+
339355
# Run entire tests for each workflow
340356
if not priority_tests and not default_list and not distributed_list and not inductor_list:
341357
# run entire tests for default, distributed and inductor workflows → use test.sh

0 commit comments

Comments
 (0)