| 1 |
# Copyright 2022 The HuggingFace Team. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
import json |
| 15 |
import os |
| 16 |
from datetime import date |
| 17 |
from pathlib import Path |
| 18 |
|
| 19 |
from tabulate import DataRow, TableFormat, tabulate |
| 20 |
|
| 21 |
|
| 22 |
hf_table_format = TableFormat( |
| 23 |
lineabove=None, |
| 24 |
linebelowheader=None, |
| 25 |
linebetweenrows=None, |
| 26 |
linebelow=None, |
| 27 |
headerrow=DataRow("", "|", "|"), |
| 28 |
datarow=DataRow("", "|", "|"), |
| 29 |
padding=1, |
| 30 |
with_header_hide=None, |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
failed = [] |
| 35 |
group_info = [] |
| 36 |
|
| 37 |
no_error_payload = {"type": "section", "text": {"type": "plain_text", "text": "No failed tests! 🤗", "emoji": True}} |
| 38 |
|
| 39 |
payload = [ |
| 40 |
{ |
| 41 |
"type": "header", |
| 42 |
"text": { |
| 43 |
"type": "plain_text", |
| 44 |
"text": f"🤗 Accelerate nightly {os.environ.get('TEST_TYPE', '')} test results", |
| 45 |
"emoji": True, |
| 46 |
}, |
| 47 |
} |
| 48 |
] |
| 49 |
|
| 50 |
total_num_failed = 0 |
| 51 |
for log in Path().glob("*.log"): |
| 52 |
section_num_failed = 0 |
| 53 |
with open(log) as f: |
| 54 |
for line in f: |
| 55 |
line = json.loads(line) |
| 56 |
if line.get("nodeid", "") != "": |
| 57 |
test = line["nodeid"] |
| 58 |
if line.get("duration", None) is not None: |
| 59 |
duration = f"{line['duration']:.4f}" |
| 60 |
if line.get("outcome", "") == "failed": |
| 61 |
section_num_failed += 1 |
| 62 |
failed.append([test, duration, log.name.split("_")[0]]) |
| 63 |
total_num_failed += 1 |
| 64 |
group_info.append([str(log), section_num_failed, failed]) |
| 65 |
failed = [] |
| 66 |
log.unlink() |
| 67 |
|
| 68 |
message = "" |
| 69 |
all_files2failed = [] |
| 70 |
if total_num_failed > 0: |
| 71 |
for name, num_failed, failed_tests in group_info: |
| 72 |
if num_failed > 0: |
| 73 |
if num_failed == 1: |
| 74 |
message += f"*{name[1:]}: {num_failed} failed test*\n" |
| 75 |
else: |
| 76 |
message += f"*{name[1:]}: {num_failed} failed tests*\n" |
| 77 |
failed_table = [] |
| 78 |
files2failed = {} |
| 79 |
for test in failed_tests: |
| 80 |
data = test[0].split("::") |
| 81 |
data[0] = data[0].split("/")[-1] |
| 82 |
if data[0] not in files2failed: |
| 83 |
files2failed[data[0]] = [data[1:]] |
| 84 |
else: |
| 85 |
files2failed[data[0]] += [data[1:]] |
| 86 |
failed_table.append(data) |
| 87 |
|
| 88 |
files = [test[0] for test in failed_table] |
| 89 |
individual_files = list(set(files)) |
| 90 |
# Count number of instances in failed_tests |
| 91 |
table = [] |
| 92 |
for file in individual_files: |
| 93 |
table.append([file, len(files2failed[file])]) |
| 94 |
|
| 95 |
failed_table = tabulate( |
| 96 |
table, |
| 97 |
headers=["Test Location", "Num Failed"], |
| 98 |
tablefmt=hf_table_format, |
| 99 |
stralign="right", |
| 100 |
) |
| 101 |
message += f"\n```\n{failed_table}\n```" |
| 102 |
all_files2failed.append(files2failed) |
| 103 |
if len(message) > 3000: |
| 104 |
err = "Too many failed tests, please see the full report in the Action results." |
| 105 |
offset = len(err) + 10 |
| 106 |
message = message[: 3000 - offset] + f"\n...\n```\n{err}" |
| 107 |
print(f"### {message}") |
| 108 |
else: |
| 109 |
message = "No failed tests! 🤗" |
| 110 |
print(f"## {message}") |
| 111 |
payload.append(no_error_payload) |
| 112 |
|
| 113 |
if os.environ.get("TEST_TYPE", "") != "": |
| 114 |
from slack_sdk import WebClient |
| 115 |
|
| 116 |
client = WebClient(token=os.environ["SLACK_API_TOKEN"]) |
| 117 |
if message != "No failed tests! 🤗": |
| 118 |
md_report = { |
| 119 |
"type": "section", |
| 120 |
"text": { |
| 121 |
"type": "mrkdwn", |
| 122 |
"text": message, |
| 123 |
}, |
| 124 |
} |
| 125 |
payload.append(md_report) |
| 126 |
action_button = { |
| 127 |
"type": "section", |
| 128 |
"text": { |
| 129 |
"type": "mrkdwn", |
| 130 |
"text": "*For more details:*", |
| 131 |
}, |
| 132 |
"accessory": { |
| 133 |
"type": "button", |
| 134 |
"text": { |
| 135 |
"type": "plain_text", |
| 136 |
"text": "Check Action results", |
| 137 |
"emoji": True, |
| 138 |
}, |
| 139 |
"url": f"https://github.com/{os.environ['GITHUB_REPOSITORY']}/actions/runs/{os.environ['GITHUB_RUN_ID']}", |
| 140 |
}, |
| 141 |
} |
| 142 |
payload.append(action_button) |
| 143 |
date_report = { |
| 144 |
"type": "context", |
| 145 |
"elements": [ |
| 146 |
{ |
| 147 |
"type": "plain_text", |
| 148 |
"text": f"Nightly {os.environ.get('TEST_TYPE')} test results for {date.today()}", |
| 149 |
} |
| 150 |
], |
| 151 |
} |
| 152 |
payload.append(date_report) |
| 153 |
response = client.chat_postMessage(channel="#accelerate-ci-daily", text=message, blocks=payload) |
| 154 |
ts = response.data["ts"] |
| 155 |
for failed_file in all_files2failed: |
| 156 |
for test_location, test_failures in failed_file.items(): |
| 157 |
# Keep only the first instance of the test name |
| 158 |
test_class = "" |
| 159 |
for i, row in enumerate(test_failures): |
| 160 |
if row[0] != test_class: |
| 161 |
test_class = row[0] |
| 162 |
else: |
| 163 |
test_failures[i][0] = "" |
| 164 |
|
| 165 |
payload = { |
| 166 |
"type": "section", |
| 167 |
"text": { |
| 168 |
"type": "mrkdwn", |
| 169 |
"text": f"Test location: {test_location}\n```\n{tabulate(test_failures, headers=['Class', 'Test'], tablefmt=hf_table_format, stralign='right')}\n```", |
| 170 |
}, |
| 171 |
} |
| 172 |
|
| 173 |
client.chat_postMessage( |
| 174 |
channel="#accelerate-ci-daily", |
| 175 |
thread_ts=ts, |
| 176 |
blocks=[payload], |
| 177 |
) |