文章目录
- [Inference Performance Comparison(推理性能对比实验)](#Inference Performance Comparison(推理性能对比实验))
Inference Performance Comparison(推理性能对比实验)
控制单一变量,标准化评测推理方案,量化对比 latency / 吞吐 / 显存,选出最优推理策略。

代码实现:
python
import time
python
# TODO 1: 统计平均 benchmark 耗时
def benchmark_fn(fn, warmup=3, iters=10):
"""Measure average runtime after warmup."""
for _ in range(warmup):
fn()
start = time.perf_counter()
for _ in range(iters):
fn()
end = time.perf_counter()
total = end - start
return total / iters
# TODO 2: 汇总推理指标
def summarize_inference_result(prefill_ms, decode_ms, peak_mem_mb, generated_tokens):
total_ms = prefill_ms + decode_ms
decode_share = decode_ms / total_ms if total_ms else 0.0
throughput_tok_s = generated_tokens / (total_ms / 1000.0) if total_ms and generated_tokens else 0.0
return {
'prefill_ms': round(prefill_ms, 2),
'decode_ms': round(decode_ms, 2),
'throughput_tok_s': round(throughput_tok_s, 2),
'total_ms': round(total_ms, 2),
'decode_share': round(decode_share, 3),
'peak_mem_mb': round(peak_mem_mb, 2),
}
# TODO 3: 生成 baseline vs candidate 的对比表
def format_comparison_report(baseline_name, baseline_summary, candidate_name, candidate_summary):
rows = [
(baseline_name, baseline_summary),
(candidate_name, candidate_summary),
]
header = "| 配置 | prefill latency | decode latency | throughput(tok/s) | peak memory | 备注 |"
sep = "| --- | --- | --- | --- | --- | --- |"
lines = [header, sep]
for name, summary in rows:
report_row = f"| {name} | {summary['prefill_ms']} | {summary['decode_ms']} | {summary['throughput_tok_s']} | {summary['peak_mem_mb']} | {summary.get('note', '')} |"
lines.append(report_row)
return "\n".join(lines)
baseline = summarize_inference_result(42.5, 18.0, 5120.0, 100)
baseline['note'] = 'Baseline'
candidate = summarize_inference_result(35.0, 15.0, 4608.0, 100)
candidate['note'] = 'Candidate'
print(format_comparison_report('Baseline', baseline, 'Candidate', candidate))
python
# 测试你的实现
def test_inference_project_template():
try:
summary = summarize_inference_result(10.0, 5.0, 256.0, 100)
assert summary['total_ms'] == 15.0, "total_ms 计算不正确!"
assert summary['decode_share'] == 0.333, "decode_share 计算不正确!"
assert summary['throughput_tok_s'] == 6666.67, "throughput_tok_s 计算不正确!"
assert summary['peak_mem_mb'] == 256.0, "peak_mem_mb 计算不正确!"
counter = {'n': 0}
def fn():
counter['n'] += 1
avg = benchmark_fn(fn, warmup=0, iters=3)
assert counter['n'] == 3, "benchmark_fn 没有正确执行迭代次数!"
assert avg >= 0.0, "benchmark_fn 的返回值应为非负数!"
report = format_comparison_report("Baseline", summary, "Candidate", summary)
assert "Baseline" in report and "Candidate" in report, "对比表未包含 baseline / candidate!"
assert "throughput(tok/s)" in report, "对比表字段不完整!"
print("✅ 推理性能对比项目模板代码通过基础校验。")
except NotImplementedError:
print("请先完成 TODO 代码!")
raise
except (AttributeError, NameError, TypeError, ValueError, AssertionError, RuntimeError) as e:
if isinstance(e, AttributeError):
print("代码未完成,无法找到必要的属性")
elif isinstance(e, NameError):
print("代码可能未完成,导致了变量未定义")
elif isinstance(e, TypeError):
print("代码可能未完成,导致了操作错误")
elif isinstance(e, ValueError):
print("代码可能未完成,导致了张量维度错误")
elif isinstance(e, AssertionError):
print(f"❌ 测试失败: {e}")
elif isinstance(e, RuntimeError):
print("代码可能未完成,导致了运行时错误")
else:
print("代码可能未完成,导致了断言失败")
raise NotImplementedError("请先完成 TODO 代码!") from e
except Exception as e:
print(f"❌ 发生未知异常: {e}")
raise
test_inference_project_template()
结果:

