欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 【知识扫盲】如何由inq,ouq和totaltime计算tokens/s

【知识扫盲】如何由inq,ouq和totaltime计算tokens/s

2025/6/7 10:25:33 来源:https://blog.csdn.net/xyz3120/article/details/148483245  浏览:    关键词:【知识扫盲】如何由inq,ouq和totaltime计算tokens/s

1 问题

由Batchsize,In_seq ,Out_seq,Total time(s),First token time ,Non-first token time这些参数,怎么计算tokens/s?

1.1 参数解释

  • Batchsize:一个批次中包含的样本数量;
  • In_seq(输入序列长度):每个样本的输入 token 数量;
  • Out_seq(输出序列长度):每个样本生成的输出 token 数量;
  • Total time(s):整个 batch 完成推理/生成所用的总时间(单位:秒);
  • First token time:第一个输出 token 生成所需的时间;
  • Non-first token time:后续 token 的平均生成时间;

我们可以从不同角度来计算 tokens/s,具体取决于你要评估的是哪种性能指标。


✅ 场景一:整体吞吐量(Throughput)

这是衡量模型整体处理能力的常用指标,通常用于对比模型或系统在固定负载下的效率。

📌 公式:

Tokens per second = Batchsize × ( In_seq + Out_seq ) Total time \text{Tokens per second} = \frac{\text{Batchsize} \times (\text{In\_seq} + \text{Out\_seq})}{\text{Total time}} Tokens per second=Total timeBatchsize×(In_seq+Out_seq)

🔍 解释:

  • 每个样本有 In_seq 个输入 token 和 Out_seq 个输出 token;
  • 所以一个 batch 总共涉及的 token 数为:Batchsize × (In_seq + Out_seq)
  • 总时间是整个 batch 处理完所花的时间。

✅ 示例:

假设:

  • Batchsize = 8
  • In_seq = 128
  • Out_seq = 64
  • Total time = 1s

那么:
Tokens/s = 8 × ( 128 + 64 ) 1 = 1536 tokens/s \text{Tokens/s} = \frac{8 \times (128 + 64)}{1} = 1536 \text{ tokens/s} Tokens/s=18×(128+64)=1536 tokens/s


✅ 场景二:解码阶段吞吐量(仅看输出 token)

如果你只关心 生成阶段(即 decode 阶段)的 token 吞吐量(如大模型文本生成),可以这样算:

📌 公式:

Decoding Tokens per second = Batchsize × Out_seq Total time - First token time + Non-first token time × (Out_seq - 1) \text{Decoding Tokens per second} = \frac{\text{Batchsize} \times \text{Out\_seq}}{\text{Total time - First token time + Non-first token time × (Out\_seq - 1)}} Decoding Tokens per second=Total time - First token time + Non-first token time × (Out_seq - 1)Batchsize×Out_seq

这个公式更精确地考虑了每个 token 的生成时间(尤其适用于流式输出场景)。

但如果你只是粗略估算,也可以使用下面这个简化版本:

📌 简化公式:

Decoding Tokens per second ≈ Batchsize × Out_seq Total time \text{Decoding Tokens per second} ≈ \frac{\text{Batchsize} \times \text{Out\_seq}}{\text{Total time}} Decoding Tokens per secondTotal timeBatchsize×Out_seq


✅ 场景三:首 token 延迟(First Token Latency)

这通常是用户体验的重要指标,表示用户发出请求后多久能看到第一个字输出。

📌 公式:

First token latency = First token time \text{First token latency} = \text{First token time} First token latency=First token time

单位一般是毫秒(ms)。


✅ 场景四:生成 token 的平均延迟(Decode Latency)

如果你想知道每个输出 token 平均需要多少时间,可以用:

📌 公式:

Avg Decode Latency per token = Total time - First token time Batchsize × ( Out_seq − 1 ) \text{Avg Decode Latency per token} = \frac{\text{Total time - First token time}}{\text{Batchsize} \times (\text{Out\_seq} - 1)} Avg Decode Latency per token=Batchsize×(Out_seq1)Total time - First token time

或者如果知道非首 token 的平均时间:

Avg Decode Latency per token = Non-first token time \text{Avg Decode Latency per token} = \text{Non-first token time} Avg Decode Latency per token=Non-first token time


🔚 总结表格

指标公式
整体吞吐量(token/s) Batchsize × ( In_seq + Out_seq ) Total time \frac{\text{Batchsize} \times (\text{In\_seq} + \text{Out\_seq})}{\text{Total time}} Total timeBatchsize×(In_seq+Out_seq)
生成吞吐量(token/s) Batchsize × Out_seq Total time \frac{\text{Batchsize} \times \text{Out\_seq}}{\text{Total time}} Total timeBatchsize×Out_seq
首 token 延迟(ms) First token time \text{First token time} First token time
单个 token 平均延迟 Non-first token time \text{Non-first token time} Non-first token time

1.2 具体示例

如果提供以上数据,该如何计算Non-first token time(ms), Non-first token Throughput(Tokens/s), Throughput(Tokens/s) , Non-first token Throughput Average(Tokens/s), E2E Throughput Average(Tokens/s)这些指标呢?


✅ 输入参数(与前面定义一一对应):

参数名
Batchsize1
In_seq1024
Out_seq1024
Total time (s)19.88901
First token time (ms)115.74
Non-first token time (ms)19.25

🧮 计算各项指标


1️⃣ Non-first token Throughput (Tokens/s)

表示每个非首 token 的生成速度。

Non-first token Throughput = 1000 Non-first token time (ms) = 1000 19.25 ≈ 51.95 tokens/s \text{Non-first token Throughput} = \frac{1000}{\text{Non-first token time (ms)}} = \frac{1000}{19.25} ≈ 51.95 \text{ tokens/s} Non-first token Throughput=Non-first token time (ms)1000=19.25100051.95 tokens/s


2️⃣ Throughput (Tokens/s)

这是整个 batch 的吞吐量,包含输入和输出 token。

Throughput = Batchsize × ( In_seq + Out_seq ) Total time = 1 × ( 1024 + 1024 ) 19.88901 ≈ 2048 19.88901 ≈ 102.97 tokens/s \text{Throughput} = \frac{\text{Batchsize} \times (\text{In\_seq} + \text{Out\_seq})}{\text{Total time}} = \frac{1 \times (1024 + 1024)}{19.88901} ≈ \frac{2048}{19.88901} ≈ 102.97 \text{ tokens/s} Throughput=Total timeBatchsize×(In_seq+Out_seq)=19.889011×(1024+1024)19.889012048102.97 tokens/s


3️⃣ Non-first token Throughput Average (Tokens/s)

这个是上面第1项的重复,即每秒能生成多少个非首 token。

✅ 已经算出:51.95 tokens/s


4️⃣ E2E Throughput Average (Tokens/s)

端到端吞吐量,仅考虑输出 token(用于衡量文本生成能力)。

E2E Throughput = Batchsize × Out_seq Total time = 1 × 1024 19.88901 ≈ 1024 19.88901 ≈ 51.49 tokens/s \text{E2E Throughput} = \frac{\text{Batchsize} \times \text{Out\_seq}}{\text{Total time}} = \frac{1 \times 1024}{19.88901} ≈ \frac{1024}{19.88901} ≈ 51.49 \text{ tokens/s} E2E Throughput=Total timeBatchsize×Out_seq=19.889011×102419.88901102451.49 tokens/s


📊 最终结果汇总表:

指标名称值(保留两位小数)
Non-first token time (ms)19.25
Non-first token Throughput (Tokens/s)51.95
Throughput (Tokens/s)102.97
Non-first token Throughput Average51.95
E2E Throughput Average (Tokens/s)51.49

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词