AI Skills AI技能 4h ago Updated 1h ago 更新于 1小时前 44

Measuring Performance of Transformer Inference 测量Transformer推理性能

LLM inference optimization requires precise measurement across multiple dimensions: latency, throughput, memory, and cost per token, as optimizing one metric can degrade another Time to first token (TTFT) and time per output token (TPOT) are critical user-facing metrics that must be tracked separately from aggregate latency Prefill and decode phases have fundamentally different performance characteristics and should be measured independently, as they stress different hardware bottlenecks Tail la LLM推理性能需要多维度测量,包括延迟、吞吐量、TTFT、TPOT、内存使用率和每token成本 单一延迟指标不足以评估LLM性能,需区分prefill和decode阶段,并记录prompt tokens和output tokens 应报告p90/p95/p99等高百分位延迟,避免优化平均值而恶化最坏情况 使用time.perf_counter()测量单请求性能,通过use_cache=True暴露KV cache以分离prefill和decode测量 需注意预热和同步开销,首次执行可能因JIT编译等导致结果失真

58
Hot 热度
70
Quality 质量
62
Impact 影响力

Analysis 深度分析

TL;DR

  • LLM inference optimization requires precise measurement across multiple dimensions: latency, throughput, memory, and cost per token, as optimizing one metric can degrade another
  • Time to first token (TTFT) and time per output token (TPOT) are critical user-facing metrics that must be tracked separately from aggregate latency
  • Prefill and decode phases have fundamentally different performance characteristics and should be measured independently, as they stress different hardware bottlenecks
  • Tail latency (p90, p95, p99) is essential for understanding real-world user experience, not just mean or median performance
  • CUDA events and proper synchronization are necessary for accurate GPU inference benchmarking, and time.perf_counter() is recommended over time.time() for Python-based measurements

Why It Matters

This guide addresses a fundamental gap in LLM deployment: without rigorous performance measurement, practitioners risk optimizing for the wrong metrics, leading to systems that appear faster in benchmarks but deliver worse user experience. As LLM services scale, understanding the tradeoffs between TTFT, TPOT, throughput, and cost per token directly impacts both user satisfaction and operational economics.

Technical Details

  • Core Metrics: Latency (total request time), TTFT (wall time until first token), TPOT (average inter-token generation time), throughput (tokens/requests per second), memory usage, accelerator utilization, and cost per token
  • Prefill vs. Decode Separation: The article provides a Python implementation using Hugging Face transformers that manually separates prefill (processing the full prompt) from decode (iterative token generation with KV cache), avoiding the generate() method to expose both phases independently
  • Tail Latency Reporting: Recommends reporting p90, p95, and p99 percentiles alongside mean/median using NumPy, with a utility function that prevents the common pitfall of optimizing averages while worsening worst-case scenarios
  • GPU Benchmarking: Highlights the need for CUDA events and torch.cuda.synchronize() to ensure accurate GPU timing, as GPU operations are asynchronous and wall-clock measurements without synchronization can be misleading
  • Multi-request and Multi-GPU Considerations: The chapter structure indicates coverage of concurrent request benchmarking, multi-GPU setups, and multi-machine distributed inference, along with cost-per-token calculations for economic evaluation

Industry Insight

  • Organizations should establish standardized benchmarking pipelines that report both head-line metrics (mean latency, throughput) and tail metrics (p99) before and after any optimization, as many published "speedups" only improve averages while degrading worst-case latency
  • The prefill/decode separation is critical for capacity planning: prefill-bound workloads (long prompts, short outputs) and decode-bound workloads (short prompts, long outputs) require different hardware configurations and scaling strategies
  • Cost-per-token metrics should be integrated into performance dashboards alongside latency numbers, as the most "performant" optimization may be economically unsustainable at scale; this bridges the gap between engineering and business stakeholders

TL;DR

  • LLM推理性能需要多维度测量,包括延迟、吞吐量、TTFT、TPOT、内存使用率和每token成本
  • 单一延迟指标不足以评估LLM性能,需区分prefill和decode阶段,并记录prompt tokens和output tokens
  • 应报告p90/p95/p99等高百分位延迟,避免优化平均值而恶化最坏情况
  • 使用time.perf_counter()测量单请求性能,通过use_cache=True暴露KV cache以分离prefill和decode测量
  • 需注意预热和同步开销,首次执行可能因JIT编译等导致结果失真

为什么值得看

本文为LLM推理优化提供了系统性的性能测量方法论,帮助从业者避免"优化后模型更复杂但速度未提升"的常见陷阱。对AI工程师和研究人员而言,掌握科学的基准测试方法是提升生产环境推理效率的关键基础。

技术解析

  • 核心性能指标:延迟(Latency)、首token时间(TTFT)、每输出token时间(TPOT)、吞吐量(Throughput)、内存使用率、加速器利用率和每token成本。需分别记录prompt tokens和output tokens数量,因为Request A(2000 prompt + 20 output)压力在prefill,Request B(20 prompt + 2000 output)压力在decode。
  • 尾延迟测量:使用NumPy计算p90/p95/p99百分位延迟,避免仅优化平均值而忽视最坏情况。示例代码展示了如何统计mean、median和高分位延迟。
  • 单请求测量实现:通过手动控制推理循环而非使用model.generate(),分别测量prefill和decode阶段。关键技巧包括use_cache=True返回KV cache,decode阶段只传入next_token而非完整序列。
  • 预热与同步:首次执行可能因模块导入、JIT编译等产生一次性开销,需进行预热。GPU测量需使用torch.cuda.synchronize()确保同步,避免异步执行导致的计时不准确。

行业启示

  • 建立标准化的LLM推理基准测试框架是生产部署的前提,应同时关注用户可见延迟(TTFT/TPOT)和运营商关注的指标(吞吐量/内存/成本)
  • 优化推理性能时需平衡多维度指标,避免单一优化导致其他方面恶化,建议采用多维度评估而非单一延迟指标
  • 随着多GPU和多机部署成为常态,性能测量需扩展到分布式场景,考虑通信开销和负载均衡对整体性能的影响

Disclaimer: The above content is generated by AI and is for reference only. 免责声明:以上内容由 AI 生成,仅供参考。

LLM 大模型 Inference 推理 Evaluation 评测