Download app/src/token-rate.mjs from Mike0021/MiniCPM5-2B-WebGPU-Pi-HTTP: direct link, hf CLI and curl.
- Browser
- Download file 931 Bytes
-
https://huggingface.co/spaces/Mike0021/MiniCPM5-2B-WebGPU-Pi-HTTP/resolve/main/app/src/token-rate.mjs
- Command line
-
hf download hf://spaces/Mike0021/MiniCPM5-2B-WebGPU-Pi-HTTP/app/src/token-rate.mjs
-
curl -L -o token-rate.mjs https://huggingface.co/spaces/Mike0021/MiniCPM5-2B-WebGPU-Pi-HTTP/resolve/main/app/src/token-rate.mjs
931 Bytes
| // Decode throughput over a rolling second. The first token establishes the | |
| // baseline, so prompt processing and first-token latency are not counted. | |
| export class TokenRateWindow { | |
| constructor(windowMs = 1000, minimumMs = 250) { | |
| this.windowMs = windowMs; | |
| this.minimumMs = minimumMs; | |
| this.total = 0; | |
| this.firstAt = null; | |
| this.points = []; | |
| } | |
| add(count, at) { | |
| if (!(count > 0)) return; | |
| this.firstAt ??= at; | |
| this.total += count; | |
| this.points.push({ at, total: this.total }); | |
| this.prune(at); | |
| } | |
| prune(at) { | |
| const cutoff = at - this.windowMs; | |
| while (this.points.length > 1 && this.points[1].at <= cutoff) this.points.shift(); | |
| } | |
| rate(at) { | |
| if (this.firstAt === null || at - this.firstAt < this.minimumMs) return null; | |
| this.prune(at); | |
| const elapsed = Math.min(this.windowMs, at - this.firstAt); | |
| return (this.total - this.points[0].total) * 1000 / elapsed; | |
| } | |
| } | |