forked from modular/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkdiff.py
More file actions
278 lines (232 loc) · 7.8 KB
/
kdiff.py
File metadata and controls
278 lines (232 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2026, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
import os
import subprocess
import sys
from time import sleep
import click
import yaml
def shell(arg_str: str, check: bool = False, verbose=True): # noqa: ANN001, ANN201
if not arg_str:
return None
print(f"$ [{arg_str}]")
p = subprocess.run(
arg_str,
shell=True,
check=check,
capture_output=True,
encoding="utf-8",
)
if verbose and p.stderr:
print("> {p.stderr}")
return p.stdout.split("\n")
def export_env(key, val) -> None: # noqa: ANN001
os.environ[key] = val
def export_arg_env() -> None:
"""
export ARGO_SERVER='argo-workflows-staging-release-cea88dd5-server.argo.svc.platform-staging-karpenter-eks-cluster-3b48ea0.local:80'
export ARGO_HTTP1=true
export ARGO_SECURE=false
export ARGO_BASE_HREF=
export ARGO_TOKEN=''
export ARGO_NAMESPACE=argo ; # or whatever your namespace is
export KUBECONFIG=/dev/null ; # recommended
"""
export_list = [
[
"ARGO_SERVER",
"argo-workflows-staging-release-cea88dd5-server.argo.svc.platform-staging-karpenter-eks-cluster-3b48ea0.local:80",
],
["ARGO_HTTP1", "true"],
["ARGO_SECURE", "false"],
["ARGO_BASE_HREF", ""],
["ARGO_TOKEN", ""],
["ARGO_NAMESPACE", "argo"],
["KUBECONFIG", "/dev/null"],
]
for k, v in export_list:
export_env(k, v)
def check_argo_workflow_exists(git_sha): # noqa: ANN001, ANN201
# check if workflow exists and return the output of argo list as yaml else None
result = shell(
f'argo list --prefix "kernels-{git_sha}" --completed -o yaml'
)
if result[0] != "[]":
result_yaml = yaml.safe_load("\n".join(result))
return result_yaml
return None
def search_workflows(branch_sha, last_n_commits=100, timeout_secs=60): # noqa: ANN001, ANN201
print(
f"Checking {last_n_commits} of origin/main for existing CI workflows"
" (baseline)"
)
main_git_sha_list = shell(
f"git log --pretty=format:'%h' -{last_n_commits} origin/main"
)
main_sha = None
for git_sha in main_git_sha_list:
ref_main_yaml = check_argo_workflow_exists(git_sha=git_sha)
if ref_main_yaml:
main_sha = git_sha
print(f"[Found /origin/main argo workflow for sha {main_sha}]")
break
while True:
ref_branch_yaml = check_argo_workflow_exists(branch_sha)
if not ref_branch_yaml:
print(f"couldn't find, check back in {timeout_secs} seconds")
sleep(timeout_secs)
else:
break
return [main_sha, ref_main_yaml, ref_branch_yaml]
def download_artifacts(
target_name, # noqa: ANN001
main_sha, # noqa: ANN001
branch_sha, # noqa: ANN001
ref_main_yaml, # noqa: ANN001
ref_branch_yaml, # noqa: ANN001
output_dir, # noqa: ANN001
extension, # noqa: ANN001
) -> None:
# print("found workflow for ", current_sha)
# TODO: convert to PATH
shell(f"mkdir -p {output_dir}/main")
shell(f"mkdir -p {output_dir}/branch")
artifact_name = "result-dir"
shell(
f"argo cp {ref_main_yaml[0]['metadata']['name']} --artifact-name"
f" {artifact_name} {output_dir}/main"
)
shell(
f"argo cp {ref_branch_yaml[0]['metadata']['name']} --artifact-name"
f" {artifact_name} {output_dir}/branch"
)
result_main = shell(f"find {output_dir}/main/argo/|grep result-dir.tgz")
result_branch = shell(f"find {output_dir}/branch/argo/|grep result-dir.tgz")
shell(f"tar -xf {result_main[0]} -C {output_dir}")
shell(f"tar -xf {result_branch[0]} -C {output_dir}")
main_sha8 = main_sha[:8]
branch_sha8 = branch_sha[:8]
target_path_main = shell(
f"find {output_dir}/benchmark-results/_{main_sha8}/|grep"
f" {target_name}|grep csv"
)
target_path_branch = shell(
f"find {output_dir}/benchmark-results/_{branch_sha8}/|grep"
f" {target_name}|grep csv"
)
kp = shell(
"./bazelw run -- //max/kernels/benchmarks/autotune:kplot"
f" {target_path_main[0]} {target_path_branch[0]} "
f"--label=main/{target_name} --label=branch/{target_name} "
f" -o {output_dir}/main_vs_branch_{target_name} -x {extension}"
)
print("\n".join(kp))
def compare_to_main(target_name, branch_sha, output_dir, extension) -> None: # noqa: ANN001
export_arg_env()
main_sha, ref_main_yaml, ref_branch_yaml = search_workflows(
branch_sha=branch_sha
)
download_artifacts(
target_name=target_name,
main_sha=main_sha,
branch_sha=branch_sha,
ref_main_yaml=ref_main_yaml,
ref_branch_yaml=ref_branch_yaml,
output_dir=output_dir,
extension=extension,
)
help_str = """
kdiff: compare performance with origin/main
- Setup gh
gh auth login
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser
! First copy your one-time code: ABCD-1234
- Press Enter to open github.com in your browser...
"""
@click.command(help=help_str, no_args_is_help=True)
@click.option(
"--run", "-r", "run_branch", help="Run the workflow on CI.", multiple=False
)
@click.option(
"--output-dir",
"-o",
"output_path",
default="./",
help="Path to output directory (tmp).",
)
@click.option(
"--extension",
"-x",
"extension",
default="png",
type=click.STRING,
help="output extension", # TODO: complete docstring
)
@click.option(
"--target",
"targets",
default=(),
help="Set extra params from CLI.",
multiple=True,
)
@click.option(
"--verbose", "-v", is_flag=True, default=False, help="Verbose printing."
)
@click.argument("branch_sha", nargs=-1, type=click.UNPROCESSED)
def cli(
branch_sha, # noqa: ANN001
run_branch, # noqa: ANN001
output_path, # noqa: ANN001
extension, # noqa: ANN001
targets, # noqa: ANN001
verbose, # noqa: ANN001
) -> None:
assert len(branch_sha) == 1
assert output_path
assert len(targets) > 0
branch_sha = shell(f"git rev-parse --short=8 {branch_sha[0]}")[0]
branch_name_list = shell(f"git branch -a --contains {branch_sha}")
found = False
if run_branch:
for b in branch_name_list:
if run_branch in b:
found = True
break
assert found
print("Kickoff")
workflow_run = shell(
f'gh workflow run "Scheduled Kernels Benchmarks" --ref {run_branch}'
)
print(
"Check here"
" [https://github.com/modularml/modular/actions/workflows/scheduled-kernels-benchmarks.yaml]"
)
if not verbose:
sys.tracebacklimit = 1
for tn in targets:
compare_to_main(
target_name=tn,
branch_sha=branch_sha,
output_dir=output_path,
extension=extension,
)
def main() -> None:
cli()
if __name__ == "__main__":
if directory := os.getenv("BUILD_WORKSPACE_DIRECTORY"):
os.chdir(directory)
main()