-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathcalculate.py
More file actions
144 lines (116 loc) · 5.34 KB
/
Copy pathcalculate.py
File metadata and controls
144 lines (116 loc) · 5.34 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
# Copyright (c) 2024 BAAI. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License")
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import time
from loguru import logger
from triton.testing import do_bench as kernel_bench
import os
import subprocess
# test operation correctness
def do_correctness(operation):
flaggems_dir = os.getenv("FLAGGEMS_WORK_DIR", "/")
gems_repo = subprocess.check_output(
["find", flaggems_dir, "-type", "d", "-name", "FlagGems"], text=True).strip()
p = subprocess.Popen(
f"cd {os.path.join(gems_repo, 'tests')} && python3 test_named_ops.py --name {operation} --device cpu ",
shell=True
)
p.wait()
return p.returncode
# test operation performance
def do_performance(mode, warmup, result_log_dir):
flaggems_dir = os.getenv("FLAGGEMS_WORK_DIR", "/")
gems_repo = subprocess.check_output(
["find", flaggems_dir, "-type", "d", "-name", "FlagGems"], text=True).strip()
p = subprocess.Popen(
# 执行所有算子
f"cd {os.path.join(gems_repo, 'benchmark')} && pytest --level core --mode {mode} --warmup {warmup} --record log",
# 执行单个算子
# f"cd {os.path.join(gems_repo, 'benchmark')} && pytest -m mm --level core --mode {mode} --warmup {warmup} --record log -s",
# 执行文件
# f"cd {os.path.join(gems_repo, 'benchmark')} && pytest test_tensor_concat_perf.py --level core --mode {mode} --warmup {warmup} --record log",
shell=True
)
p.wait()
# log_dir = os.path.join(gems_repo, "benchmark", "result--level_core--record_log")
# log_dir = os.path.join(gems_repo, "benchmark",
# f"result_test_tensor_concat_perf--level_core--mode_{mode}--warmup_{warmup}--record_log.log")
log_dir = os.path.join(gems_repo, "benchmark", f"result--level_core--mode_{mode}--warmup_{warmup}--record_log.log")
save_log_path = os.path.join(result_log_dir, "result.log.txt")
logger.info("======print do_performance save_log_path============")
logger.info(save_log_path)
with open(log_dir, "r", encoding="utf-8") as file_r, open(save_log_path, "w", encoding="utf-8") as file_w:
for line in file_r:
file_w.write(line + '\n')
return p.returncode
grad_outputs = None
def do(exec_func, exec_args, bp=False):
global grad_outputs
if bp:
import torch
_tensor = exec_func(*exec_args).sum()
if grad_outputs is None:
grad_outputs = torch.zeros_like(_tensor)
inputs = list(filter(lambda x: x.requires_grad, [*exec_args]))
_grad = torch.autograd.grad(outputs=_tensor, inputs=inputs, grad_outputs=grad_outputs)
else:
_tensor = exec_func(*exec_args)
def do_test(exec_func, exec_args, sync_func, config, case_config, bp=False):
sync_func(config.vendor)
start_latency_nowarm = time.perf_counter_ns()
_tensor = exec_func(*exec_args)
sync_func(config.vendor)
latency_nowarm = time.perf_counter_ns() - start_latency_nowarm
for _ in range(case_config.WARMUP):
do(exec_func, exec_args, bp)
sync_func(config.vendor)
start_latency_warm = time.perf_counter_ns()
_tensor = exec_func(*exec_args)
sync_func(config.vendor)
latency_warm = time.perf_counter_ns() - start_latency_warm
start_time = time.perf_counter()
for _ in range(case_config.ITERS):
do(exec_func, exec_args, bp)
sync_func(config.vendor)
end_time = time.perf_counter()
cputime_raw = end_time - start_time
kerneltime_raw = kernel_bench(lambda: do(exec_func, exec_args, bp),
warmup=case_config.KERNELWARMUP,
rep=case_config.KERNELITERS,
return_mode="median")
cputime = cputime_raw / case_config.ITERS
kerneltime = kerneltime_raw / 1000.0 # ms to s
return round(latency_nowarm / 1000.0, 2), round(latency_warm / 1000.0,
2), cputime, kerneltime
def cal_perf(cputime, kerneltime, op2flops, spectflops, bp=False):
spectflops = float(spectflops)
ctus = round(cputime * 1E6, 2)
ktus = round(kerneltime * 1E6, 2)
cps = 1.0 / cputime
kps = 1.0 / kerneltime
cflops = op2flops(cps) * (3.0 if bp else 1.0)
kflops = op2flops(kps) * (3.0 if bp else 1.0)
ctflops = round(cflops / 1E12, 2)
ktflops = round(kflops / 1E12, 2)
cfu = round(100.0 * cflops / 1E12 / spectflops, 2)
kfu = round(100.0 * kflops / 1E12 / spectflops, 2)
return ctus, ktus, cps, kps, ctflops, ktflops, cfu, kfu
def print_result(config, casename, ct, kt, cps, kps, ctflops, ktflops, cfu,
kfu, correctness, lnm, lm):
print(r"[FlagPerf Result]Operation {} in {} at {}:".format(
casename, config.oplib, config.dataformat))
print(r"[FlagPerf Result]FLOPS utilization: cputime={}%, kerneltime={}%".
format(cfu, kfu))
print(
r"[FlagPerf Result]cputime={} us, throughput={} op/s, equals to {} TFLOPS"
.format(ct, cps, ctflops))
print(
r"[FlagPerf Result]kerneltime={} us, throughput={} op/s, equals to {} TFLOPS"
.format(kt, kps, ktflops))
print(r"[FlagPerf Result]Correctness with CPU golden Reference: {}".format(
correctness))
print(
r"[FlagPerf Result]First time latency: no warmup={} us, warmup={} us".
format(lnm, lm))