Text-to-Speech
ONNX
Vietnamese
vietnamese
flow-matching
webgpu
on-device
File size: 6,123 Bytes
511b660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Trà Đá · benchmark int8</title>
<style>
body{font:14px/1.5 system-ui,sans-serif;margin:0;padding:20px 16px;background:#f5efe3;color:#2b1e13}
@media (prefers-color-scheme:dark){body{background:#16110c;color:#f4eadb}}
table{border-collapse:collapse;font-variant-numeric:tabular-nums;margin-top:12px}td,th{border:1px solid #8885;padding:4px 10px;text-align:right}th:first-child,td:first-child{text-align:left}
button{font:inherit;padding:8px 16px;border-radius:8px;border:1px solid #8886;cursor:pointer}pre{white-space:pre-wrap;font-size:12px}
</style>
<h2>Trà Đá · int8 benchmark</h2>
<p>Cùng một đầu vào (câu ~60 ký tự, ~7 s audio). <b>WGSL int8</b>: engine WebGPU tự viết, trọng số int8 (bản demo đang dùng). <b>fp32</b> / <b>int8</b>: onnxruntime-web với ONNX fp32 và ONNX int8 động. Thời gian là trung vị 10 lần sau 2 lần khởi động; tổng = enc + 2×cfm2 + dec (một câu chế độ 2 bước). "lệch" = sai khác tương đối của magnitude giải mã so với dòng đầu tiên của cùng backend.</p>
<button id="go">Chạy</button> <span id="st"></span>
<div id="out"></div><pre id="log"></pre>
<script type="module">
import * as ort from './ort/ort.webgpu.min.mjs';
import { GpuEngine } from './wgpu_engine.js?v=11220400';
ort.env.wasm.wasmPaths = new URL('./ort/', import.meta.url).href;
ort.env.wasm.numThreads = self.crossOriginIsolated ? Math.min(8, navigator.hardwareConcurrency || 4) : 1;
const ROOT = new URL('..', import.meta.url).href;
const V = { fp32: g => `onnx/${g}.onnx`, int8: g => `onnx/${g}.int8.onnx` };
const $ = id => document.getElementById(id), log = s => { $('log').textContent += s + '\n'; };
let seed = 1; const rnd = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };
const gauss = n => Float32Array.from({ length: n }, () => Math.sqrt(-2 * Math.log(rnd() + 1e-9)) * Math.cos(2 * Math.PI * rnd()));
const N = 60, T = 200;
function inputs() {
  seed = 1;
  return {
    enc: { ids: new ort.Tensor('int64', BigInt64Array.from({ length: N }, () => BigInt(2 + Math.floor(rnd() * 30))), [1, N]),
           tones: new ort.Tensor('int64', BigInt64Array.from({ length: N }, () => BigInt(Math.floor(rnd() * 6))), [1, N]),
           spk: new ort.Tensor('float32', gauss(192), [1, 192]) },
    cfm2: { x: new ort.Tensor('float32', gauss(32 * T), [1, 32, T]), cond: new ort.Tensor('float32', gauss(256 * T), [1, 256, T]),
            t: new ort.Tensor('float32', new Float32Array([0]), [1]), r: new ort.Tensor('float32', new Float32Array([0.5]), [1]), spk: new ort.Tensor('float32', gauss(192), [1, 192]) },
    dec: { z: new ort.Tensor('float32', gauss(32 * T), [1, 32, T]) } };
}
const med = a => { const s = [...a].sort((x, y) => x - y); return s[s.length >> 1]; };
// the hand-written WGSL engine (wgpu_engine.js) on int8/trada-int8.bin; runs first so its dec output is the reference
async function benchWgsl(inp, ref) {
  const buf = await (await fetch(`${ROOT}int8/trada-int8.bin`)).arrayBuffer(), eng = await GpuEngine.create(buf), row = { ep: 'webgpu', v: 'WGSL int8 (mới)', mb: buf.byteLength / 1e6 };
  for (const g of ['enc', 'cfm2', 'dec']) {
    const ts = []; let out; for (let k = 0; k < 12; k++) { const t0 = performance.now(); out = await eng[g].run(inp[g]); const dt = performance.now() - t0; if (k >= 2) ts.push(dt); }
    row[g] = med(ts); if (g === 'dec') ref.dec = out.mag.data;
  }
  row.total = row.enc + 2 * row.cfm2 + row.dec; return row;
}
async function bench(ep, v, inp, ref) {
  const row = { ep, v }; let mb = 0;
  for (const g of ['enc', 'cfm2', 'dec']) {
    const buf = new Uint8Array(await (await fetch(`${ROOT}${V[v](g)}`)).arrayBuffer()); mb += buf.length / 1e6;
    let s;
    try { s = await ort.InferenceSession.create(buf, { executionProviders: [ep], graphOptimizationLevel: 'all' }); }
    catch (e) { row[g] = 'lỗi'; log(`${ep} ${v} ${g}: ${e.message}`); continue; }
    const ts = []; let out;
    for (let k = 0; k < 12; k++) { const t0 = performance.now(); out = await s.run(inp[g]); const dt = performance.now() - t0; if (k >= 2) ts.push(dt); }
    row[g] = med(ts);
    if (g === 'dec') { const o = out.mag.data; if (!ref.dec) ref.dec = o; else { let num = 0, den = 0; for (let i = 0; i < o.length; i++) { num += (o[i] - ref.dec[i]) ** 2; den += ref.dec[i] ** 2; } row.err = Math.sqrt(num / den); } }
    await s.release?.();
  }
  row.mb = mb; row.total = (row.enc || 0) + 2 * (row.cfm2 || 0) + (row.dec || 0); return row;
}
$('go').onclick = async () => {
  $('go').disabled = true; $('log').textContent = '';
  let gpu = false; try { gpu = !!(navigator.gpu && await navigator.gpu.requestAdapter()); } catch {}
  const eps = gpu ? ['webgpu', 'wasm'] : ['wasm']; const rows = [];
  log(`WebGPU: ${gpu ? 'có' : 'không'} · threads ${ort.env.wasm.numThreads} · ${navigator.userAgent}`);
  for (const ep of eps) { const ref = {}; if (ep === 'webgpu') { $('st').textContent = 'WGSL int8…'; rows.push(await benchWgsl(inputs(), ref)); render(rows); }
    for (const v of Object.keys(V)) { $('st').textContent = `${ep} · ${v}…`; const inp = inputs(); rows.push(await bench(ep, v, inp, ref)); render(rows); if (location.hash === '#auto') fetch('../_r?' + encodeURIComponent(JSON.stringify(rows.at(-1)))).catch(() => {}); } }
  $('st').textContent = 'Xong. Chụp màn hình bảng này gửi lại nhé.'; $('go').disabled = false; document.title = 'DONE';
};
if (location.hash === '#auto') setTimeout(() => $('go').click(), 100);
const f = x => typeof x === 'number' ? x.toFixed(1) : (x ?? '');
function render(rows) {
  $('out').innerHTML = `<table><tr><th>Backend · bản</th><th>MB</th><th>enc ms</th><th>cfm2 ms</th><th>dec ms</th><th>tổng (enc+2·cfm2+dec)</th><th>lệch</th></tr>` +
    rows.map(r => `<tr><td>${r.ep} · ${r.v}</td><td>${f(r.mb)}</td><td>${f(r.enc)}</td><td>${f(r.cfm2)}</td><td>${f(r.dec)}</td><td>${f(r.total)}</td><td>${r.err === undefined ? '—' : r.err.toExponential(1)}</td></tr>`).join('') + '</table>';
}
</script>