DataWhale组队学习笔记--llm-algo-leetcode(七)

文章目录

  • [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()

结果:

相关推荐
10年前端老司机2 分钟前
RAG 检索效果量化测评落地:完整流程、指标实现与踩坑总结
人工智能·llm·aigc
武子康2 分钟前
权重装进了 24 GiB,四路 32K 上下文还装得下吗?
人工智能·llm·agent
Python私教2 分钟前
DeepSeek本地部署Ollama+知识库,附3个报错解决
人工智能·llm·deepseek
闪学it4 分钟前
闪学it-Linux云计+AIOps大模型
llm
武子康4 分钟前
Codex 只读审查的权限边界:任务文件是谁写的?
人工智能·llm·agent
柒和远方10 分钟前
混合检索 RAG 全链路:查询增强、双路召回与重排——向量库和搜索引擎联手补齐召回
elasticsearch·langchain·llm
货拉拉技术19 分钟前
货拉拉 DataAgent 实践:策略复盘的智能化探索
llm·agent
犀利豆21 分钟前
AI 老板五分钟挂出招聘启事,四个月后开除了第一个人
人工智能·llm·aigc
桃西西呀21 分钟前
从手写数字到ImageNet冠军:CNN架构20年,为什么越深越难训?
人工智能·计算机视觉·llm
桃西西呀23 分钟前
给告警加了道 AI 初筛,我终于半夜不用爬起来了看无用告警了
人工智能·llm·ai编程