Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions base_test/matmul_test/READE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Matmul 自动化测试
# 1. 脚本说明
matmul 放置位置:
```shell
# mudnn_bench 默认存放在 /usr/local/musa/ 下
mudnn_bench
├── bench_test_matmul.sh
├── bin
│ ├── mudnn_bench -> mudnn_bench-x.x.x
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可能会confuse,要不写一下mudnn_bench的位置,否则需要手动安装

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

补充mudnn_bench在/usr/local/musa下

│ └── mudnn_bench-x.x.x
├── matmul_test
```
mudnn_bench 示例:
**部分旧版本mudnn_bench和mudnn版本不支持混合精度测试,需要和开发者做确认.**
```shell

# 示例 1:单卡,大矩阵,f32
MUSA_VISIBLE_DEVICES=4 ./bin/mudnn_bench -m --mm_m 6144 --mm_n 3584 --mm_k 6144 --warmup 30 --tm i --tmv 1000 -p -t f32

# 示例 2:多卡,标准尺寸,bf16
MUSA_VISIBLE_DEVICES=0,1 ./bin/mudnn_bench -m --mm_m 4096 --mm_n 4096 --mm_k 4096 --warmup 30 --tm i --tmv 1000 -p -t bf16

# 示例 3:单卡,特殊组合,int8
MUSA_VISIBLE_DEVICES=2 ./bin/mudnn_bench -m --mm_m 8192 --mm_n 8192 --mm_k 768 --warmup 30 --tm i --tmv 1000 -p -t int8

# 示例 4:使用混合精度格式
MUSA_VISIBLE_DEVICES=3 ./bin/mudnn_bench -m --mm_m 2048 --mm_n 2048 --mm_k 2048 --warmup 30 --tm i --tmv 1000 -p -t bf16:q4:bf16:bf16
```

# 2. 测试
可在测试脚本中自行批量配置测试MNK,warmup,iter等。
## 2.1 fp64, tf32 测试
注意:fp64和tf32 数据类型调用非 mudnn 接口
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tf32?还是fp32

```shell
# 1. 编译
cd ./fp64_tf32_src
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上


bash build_gemm_tf32.sh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

bash build_gemm_fp64.sh

## 2. 测试
bash test_gemm_fp64_tf32.sh
```

## 2.2 f32_f16_bf16_q8_fp8 测试
mudnn_bench 测试矩阵value默认说明:
- 浮点:-0.5~0.5
- fp8: 整型-10~10转浮点
- qint4:-7~7
- 整型:-127~127
> 部分版本 mudnn_bench 工具支持全 0 测试(参数 `-z` 实现),需要和开发者确认
```shell
bash test_gemm_f32_f16_bf16_q8_fp8.sh
```

## 2.3 混合精度测试
```shell
# A,B: fp16, C,D: f32: "f16:f16:f32:f32"
# A,B: bf16, C,D: f32: "bf16:bf16:f32:f32"
# A,B: tf32, C,D: f32: "f32"
# A,B: int8, C,D: int32: "int8"
# W8A8: "q8:q8:f32:f32"
# W4A16: "bf16:q4:bf16:bf16"
# A,B: fp8, C,D: fp16: "float8_e4m3:float8_e4m3:f16:f16"

bash test_gemm_mixed.sh
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import re
import os
import sys
from typing import List, Dict, Optional

def extract_matmul_data(log_path: str) -> List[Dict[str, str]]:
patterns = {
"datatype": re.compile(r"DataType (\w+)"),
"mat_params": re.compile(r"m (\d+), n (\d+), k (\d+)"),
"elapsed_time": re.compile(r"AverageElapsedTime\(ms\) : (\d+\.\d+)"),
"throughput_gops": re.compile(r"Throughput (\d+\.\d+) GOPS")
}

extracted = []
current_block = {}

try:
with open(log_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()

dt_match = patterns["datatype"].search(line)
if dt_match:
current_block["datatype"] = dt_match.group(1)

mp_match = patterns["mat_params"].search(line)
if mp_match:
current_block["m"] = mp_match.group(1)
current_block["n"] = mp_match.group(2)
current_block["k"] = mp_match.group(3)

et_match = patterns["elapsed_time"].search(line)
if et_match:
current_block["elapsed_time"] = et_match.group(1)

tp_match = patterns["throughput_gops"].search(line)
if tp_match:
tops = round(float(tp_match.group(1)) / 1000, 4)
current_block["throughput_tops"] = str(tops)

if line == "==============================" and current_block:
required = ["datatype", "m", "n", "k", "elapsed_time", "throughput_tops"]
if all(key in current_block for key in required):
dim = f"{current_block['m']}-{current_block['n']}-{current_block['k']}"
extracted.append({
"datatype": current_block["datatype"],
"shape": dim,
"Throughput(TOPS)": current_block["throughput_tops"],
"AverageElapsedTime(ms)": current_block["elapsed_time"]
})
current_block = {}

required = ["datatype", "m", "n", "k", "elapsed_time", "throughput_tops"]
if current_block and all(key in current_block for key in required):
dim = f"{current_block['m']}×{current_block['n']}×{current_block['k']}"
extracted.append({
"datatype": current_block["datatype"],
"shape": dim,
"Throughput(TOPS)": current_block["throughput_tops"],
"AverageElapsedTime(ms)": current_block["elapsed_time"]
})

except Exception as e:
print(f"❌ 读取日志失败:{str(e)}")
return []

return extracted

def generate_csv(data: List[Dict[str, str]], output_path: str) -> bool:
if not data:
print("⚠️ 未提取到有效数据,跳过CSV生成")
return False

headers = ["datatype", "shape", "Throughput(TOPS)", "AverageElapsedTime(ms)"]

try:
with open(output_path, 'w', encoding='utf-8') as f:
f.write(", ".join(headers) + "\n")
for item in data:
row = [item[h] for h in headers]
f.write(", ".join(row) + "\n")
print(f"✅ CSV生成成功:{output_path}")
return True
except Exception as e:
print(f"❌ 生成CSV失败:{str(e)}")
return False

def main(input_log: str, output_csv: Optional[str] = None):
if not os.path.isfile(input_log):
print(f"❌ 输入日志文件不存在:{input_log}")
return

if not output_csv:
log_dir = os.path.dirname(input_log)
log_name = os.path.splitext(os.path.basename(input_log))[0]
output_csv = os.path.join(log_dir, f"{log_name}_summary.csv")

print(f"📊 开始提取日志数据:{input_log}")
matmul_data = extract_matmul_data(input_log)

if not matmul_data:
print("❌ 未提取到任何有效测试数据")
return

print(f"✅ 成功提取 {len(matmul_data)} 条测试记录")

generate_csv(matmul_data, output_csv)
print("🎯 所有操作完成!")

if __name__ == "__main__":
# 修正sys.argv判断(sys.argv[0]是脚本名,需至少传入1个输入文件路径)
if len(sys.argv) < 2:
print("用法:")
print(" python summarize_fp64_tf32_log.py <输入日志文件路径>")
print("示例:")
print(" python summarize_fp64_tf32_log.py bench.log")
sys.exit(1)

input_path = sys.argv[1]
output_path = sys.argv[1].replace('.log', '.csv') # 日志文件同名CSV输出
main(input_path, output_path)

120 changes: 120 additions & 0 deletions base_test/matmul_test/exetrct_log_tools/summarize_fp64_tf32_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import re
import sys
import os
from typing import List, Dict, Optional

def extract_matmul_data(log_path: str) -> List[Dict[str, str]]:
patterns = {
"datatype": re.compile(r"MatMul (\w+) Test \(MUSA\)"),
"mat_params": re.compile(r"m = (\d+), n = (\d+), k = (\d+)"),
"duration_us": re.compile(r"Duration:(\s*[\d\.]+) us"),
"tflops": re.compile(r"computation-\w+=(\s*[\d\.]+)")
}

extracted = []
current_block = {}

try:
with open(log_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()

dt_match = patterns["datatype"].search(line)
if dt_match:
current_block["datatype"] = dt_match.group(1)

mp_match = patterns["mat_params"].search(line)
if mp_match:
current_block["m"] = mp_match.group(1)
current_block["n"] = mp_match.group(2)
current_block["k"] = mp_match.group(3)

dur_match = patterns["duration_us"].search(line)
if dur_match:
us_val = float(dur_match.group(1).strip())
ms_val = round(us_val / 1000, 6)
current_block["duration_ms"] = str(ms_val)

tf_match = patterns["tflops"].search(line)
if tf_match:
tf_val = tf_match.group(1).strip()
current_block["tflops"] = str(round(float(tf_val), 6))

if line == "========================================" and current_block:
required = ["datatype", "m", "n", "k", "duration_ms", "tflops"]
if all(key in current_block for key in required):
shape = f"{current_block['m']}-{current_block['n']}-{current_block['k']}"
extracted.append({
"DataType": current_block["datatype"],
"shape": shape,
"Compute_ability(TFLOPS)": current_block["tflops"],
"AverageElapsedTime(ms)": current_block["duration_ms"]
})
current_block = {}

required = ["datatype", "m", "n", "k", "duration_ms", "tflops"]
if current_block and all(key in current_block for key in required):
shape = f"{current_block['m']}-{current_block['n']}-{current_block['k']}"
extracted.append({
"DataType": current_block["datatype"],
"shape": shape,
"Compute_ability(TFLOPS)": current_block["tflops"],
"AverageElapsedTime(ms)": current_block["duration_ms"]
})

except Exception as e:
print(f"❌ 读取日志失败:{str(e)}")
return []

return extracted

def generate_csv(data: List[Dict[str, str]], output_path: str) -> bool:
if not data:
print("⚠️ 未提取到有效数据,跳过CSV生成")
return False

headers = ["DataType", "shape", "Compute_ability(TFLOPS)", "AverageElapsedTime(ms)"]
try:
with open(output_path, 'w', encoding='utf-8') as f:
f.write(", ".join(headers) + "\n")
for item in data:
row = [item[h] for h in headers]
f.write(",".join(row) + "\n")
print(f"✅ CSV生成成功:{output_path}")
return True
except Exception as e:
print(f"❌ 生成CSV失败:{str(e)}")
return False

def main(input_log: str, output_csv: Optional[str] = None):
if not os.path.isfile(input_log):
print(f"❌ 输入日志文件不存在:{input_log}")
return

if not output_csv:
log_dir = os.path.dirname(input_log)
log_name = os.path.splitext(os.path.basename(input_log))[0]
output_csv = os.path.join(log_dir, f"{log_name}_summary.csv")

print(f"📊 开始提取日志数据:{input_log}")
matmul_data = extract_matmul_data(input_log)

if not matmul_data:
print("❌ 未提取到任何有效测试数据")
return

print(f"✅ 成功提取 {len(matmul_data)} 条测试记录")
generate_csv(matmul_data, output_csv)
print("🎯 所有操作完成!")

if __name__ == "__main__":
if len(sys.argv) < 2:
print("用法:")
print(" python summarize_fp64_tf32_log.py <输入日志文件路径>")
print("示例:")
print(" python summarize_fp64_tf32_log.py bench.log")
sys.exit(1)

input_path = sys.argv[1]
output_path = sys.argv[1].replace('.log', '.csv')
main(input_path, output_path)
64 changes: 64 additions & 0 deletions base_test/matmul_test/exetrct_log_tools/summary_mixed_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import re
import sys
import csv
import os

if len(sys.argv) < 2:
print("Usage: python summary_fix_data.py <log_file>")
sys.exit(1)

log_file = sys.argv[1]
print(f"📊 正在读取并解析日志:{log_file}")

if not os.path.exists(log_file):
print("❌ 日志文件不存在")
sys.exit(1)

# 收集结果
records = []

# 正则模式
re_start = re.compile(r"测试:\s*M=(\d+),\s*N=(\d+),\s*K=(\d+),\s*Type=([\w:]+)")
re_result = re.compile(r"AverageElapsedTime\(ms\)\s*:\s*([\d\.]+)\s*,\s*Throughput\s*([\d\.]+)\s*GOPS")

cur_M = cur_N = cur_K = cur_type = None

with open(log_file, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()

# 匹配开始参数
m1 = re_start.search(line)
if m1:
cur_M, cur_N, cur_K, cur_type = m1.groups()
continue

# 匹配结果
m2 = re_result.search(line)
if m2 and cur_M is not None:
elapsed, gops = m2.groups()
records.append({
"M": cur_M,
"N": cur_N,
"K": cur_K,
"Type": cur_type,
"AvgTime(ms)": elapsed,
"GOPS": gops
})
# 清空当前块(防止串行)
cur_M = cur_N = cur_K = cur_type = None

# 输出 CSV
if not records:
print("⚠️ 未提取到任何有效数据")
sys.exit(0)

csv_path = log_file.replace(".log", ".csv")
with open(csv_path, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=records[0].keys())
writer.writeheader()
writer.writerows(records)

print(f"✅ 解析完成,共 {len(records)} 条数据")
print(f"📄 CSV 已生成:{csv_path}")

1 change: 1 addition & 0 deletions base_test/matmul_test/fp64_tf32_src/build_gemm_fp64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mcc gemm_fp64.mu -lmusart -lmublas -o gemm_fp64 --offload-arch=mp_31
1 change: 1 addition & 0 deletions base_test/matmul_test/fp64_tf32_src/build_gemm_tf32.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
g++ gemm_tf32.cpp -std=c++17 -I/usr/local/musa/include -L /usr/local/musa/lib/ -fopenmp -lmudnn -lmusart -o gemm_tf32 -O2
Loading