File size: 80,851 Bytes
a1aad67 2debebe a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 da13637 23b5df6 a1aad67 23b5df6 da13637 a1aad67 da13637 c72d46d da13637 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 c72d46d a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 c72d46d 23b5df6 a1aad67 c72d46d 23b5df6 c72d46d 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 da13637 a1aad67 23b5df6 c72d46d 23b5df6 c72d46d 23b5df6 c72d46d 23b5df6 a1aad67 c72d46d a1aad67 23b5df6 a1aad67 c72d46d a1aad67 c72d46d 23b5df6 a1aad67 23b5df6 c72d46d a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 2debebe a1aad67 303ba09 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 2debebe a1aad67 2debebe a1aad67 2debebe a1aad67 2debebe a1aad67 2debebe a1aad67 2debebe a1aad67 2debebe a1aad67 2debebe a1aad67 2debebe a1aad67 2debebe a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 2debebe 23b5df6 2debebe a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 2debebe 23b5df6 2debebe a1aad67 2debebe a1aad67 23b5df6 e07bd97 23b5df6 e07bd97 23b5df6 e07bd97 23b5df6 e07bd97 23b5df6 e07bd97 23b5df6 e07bd97 23b5df6 e07bd97 23b5df6 e07bd97 23b5df6 e07bd97 23b5df6 e07bd97 23b5df6 a1aad67 303ba09 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 23b5df6 a1aad67 | 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 | /**
* LFM2-Audio Model Runner for ONNX Runtime Web
*
* Runs audio model inference using ONNX models:
* 1. decoder.onnx - LFM2 backbone (shared with text)
* 2. audio_encoder.onnx - Conformer encoder for ASR (mel → embeddings)
* 3. audio_embedding.onnx - Audio code embeddings for TTS
* 4. audio_detokenizer.onnx - Audio codes → STFT features
* 5. vocoder_depthformer.onnx - Autoregressive codebook prediction
*
* Supports ASR mode for the webapp (transcription).
*/
import * as ort from 'onnxruntime-web';
import { AutoTokenizer, env } from '@huggingface/transformers';
import { loadMelConfig, computeMelSpectrogram, loadAudioFile } from './audio-processor.js';
// Cache configuration
const CACHE_NAME = 'onnx-models-v1';
const IDB_NAME = 'onnx-model-cache';
const IDB_STORE = 'models';
// IndexedDB helpers for fallback caching
let idbPromise = null;
function openIDB() {
if (idbPromise) return idbPromise;
idbPromise = new Promise((resolve, reject) => {
const request = indexedDB.open(IDB_NAME, 1);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result);
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(IDB_STORE)) {
db.createObjectStore(IDB_STORE);
}
};
});
return idbPromise;
}
async function idbGet(key) {
try {
const db = await openIDB();
return new Promise((resolve, reject) => {
const tx = db.transaction(IDB_STORE, 'readonly');
const store = tx.objectStore(IDB_STORE);
const request = store.get(key);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result);
});
} catch (e) {
return null;
}
}
async function idbSet(key, value) {
try {
const db = await openIDB();
return new Promise((resolve, reject) => {
const tx = db.transaction(IDB_STORE, 'readwrite');
const store = tx.objectStore(IDB_STORE);
const request = store.put(value, key);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve();
});
} catch (e) {
// Ignore cache write failures
}
}
// Special tokens for audio model
const SPECIAL_TOKENS = {
AUDIO_START: 128, // <|audio_start|>
TEXT_START: 129, // <|text_start|>
TEXT_END: 130, // <|text_end|>
MIXED_START: 131, // <|mixed_start|>
MIXED_END: 132, // <|mixed_end|>
IM_END: 7, // <|im_end|>
};
// Audio codebook constants
const NUM_CODEBOOKS = 8;
const CODEBOOK_VOCAB = 2049;
const END_OF_AUDIO_TOKEN = 2048;
// Default system prompts (matching Python lfm2-audio-infer)
const DEFAULT_SYSTEM_PROMPT_ASR = 'Perform ASR.';
const DEFAULT_SYSTEM_PROMPT_TTS = 'Perform TTS. Use the UK female voice.';
const DEFAULT_SYSTEM_PROMPT_INTERLEAVED = 'Respond with interleaved text and audio.';
// Max tokens defaults (matching liquid-audio)
// Each audio frame = 80ms (6x upsampling in detokenizer, 320 hop, 24kHz)
// 1024 frames ≈ 82 seconds of audio
const DEFAULT_MAX_TOKENS_AUDIO = 1024; // TTS and interleaved modes
const DEFAULT_MAX_TOKENS_TEXT = 100; // ASR mode
// Timestamped logging helper
let _logStartTime = null;
function log(...args) {
if (_logStartTime === null) {
_logStartTime = performance.now();
}
const elapsed = ((performance.now() - _logStartTime) / 1000).toFixed(2);
console.log(`[${elapsed}s]`, ...args);
}
function logReset() {
_logStartTime = performance.now();
}
/**
* Fetch with caching support using Cache API or IndexedDB fallback
*/
async function fetchWithCache(url, options = {}) {
if (!url.startsWith('http://') && !url.startsWith('https://')) {
return fetch(url, options);
}
const fileName = url.split('/').pop();
// Try Cache API first
if (typeof caches !== 'undefined') {
try {
const cache = await caches.open(CACHE_NAME);
const cached = await cache.match(url);
if (cached) {
console.log(`[Cache HIT] ${fileName}`);
return cached;
}
console.log(`[Cache MISS] Fetching ${fileName}...`);
const response = await fetch(url, options);
if (response.ok) {
cache.put(url, response.clone());
}
return response;
} catch (e) {
// Fall through to IndexedDB
}
}
// Try IndexedDB fallback
if (typeof indexedDB !== 'undefined') {
try {
const cached = await idbGet(url);
if (cached) {
console.log(`[IDB Cache HIT] ${fileName}`);
return new Response(cached.data, {
status: 200,
headers: { 'Content-Type': cached.contentType || 'application/octet-stream' },
});
}
console.log(`[IDB Cache MISS] Fetching ${fileName}...`);
const response = await fetch(url, options);
if (response.ok) {
const clone = response.clone();
const data = await clone.arrayBuffer();
const contentType = response.headers.get('Content-Type') || 'application/octet-stream';
await idbSet(url, { data, contentType });
}
return response;
} catch (e) {
console.warn('IndexedDB cache failed:', e);
}
}
// Direct fetch as last resort
console.log(`[No Cache] Fetching ${fileName}...`);
return fetch(url, options);
}
/**
* Clear the model cache (both Cache API and IndexedDB)
*/
export async function clearModelCache() {
let deleted = false;
// Clear Cache API
if (typeof caches !== 'undefined') {
try {
deleted = await caches.delete(CACHE_NAME);
} catch (e) {
// Ignore
}
}
// Clear IndexedDB
if (typeof indexedDB !== 'undefined') {
try {
await new Promise((resolve, reject) => {
const request = indexedDB.deleteDatabase(IDB_NAME);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve();
});
idbPromise = null; // Reset the cached promise
deleted = true;
} catch (e) {
// Ignore
}
}
console.log(deleted ? 'Model cache cleared' : 'No cache to clear');
return deleted;
}
/**
* Get cache storage usage info
*/
export async function getCacheInfo() {
if ('storage' in navigator && 'estimate' in navigator.storage) {
const estimate = await navigator.storage.estimate();
return {
used: estimate.usage || 0,
available: estimate.quota || 0,
};
}
return null;
}
/**
* Load tokenizer from model path
*/
async function loadTokenizerFromPath(modelPath) {
const isRemote = modelPath.startsWith('http://') || modelPath.startsWith('https://');
console.log(`Loading tokenizer from ${isRemote ? 'remote' : 'local'}: ${modelPath}`);
const fetchOptions = isRemote ? { mode: 'cors', credentials: 'omit' } : {};
const [tokenizerResponse, configResponse] = await Promise.all([
fetchWithCache(`${modelPath}/tokenizer.json`, fetchOptions),
fetchWithCache(`${modelPath}/tokenizer_config.json`, fetchOptions),
]);
if (!tokenizerResponse.ok) {
throw new Error(`Failed to fetch tokenizer.json: ${tokenizerResponse.status}`);
}
if (!configResponse.ok) {
throw new Error(`Failed to fetch tokenizer_config.json: ${configResponse.status}`);
}
const tokenizerJSON = await tokenizerResponse.text();
const configJSON = await configResponse.text();
// Parse tokenizer.json to extract special token IDs
const tokenizerData = JSON.parse(tokenizerJSON);
const specialTokens = {};
if (tokenizerData.added_tokens) {
for (const token of tokenizerData.added_tokens) {
specialTokens[token.content] = token.id;
}
console.log('Found special tokens:', Object.keys(specialTokens).length);
}
// Create tokenizer using transformers.js
const fakeModelId = `tokenizer-${Date.now()}`;
const fileCache = {
'tokenizer.json': tokenizerJSON,
'tokenizer_config.json': configJSON,
};
const originalFetch = globalThis.fetch;
globalThis.fetch = async (input, init) => {
const url = typeof input === 'string' ? input : input.url;
if (url.includes(fakeModelId)) {
for (const [filename, content] of Object.entries(fileCache)) {
if (url.includes(filename)) {
return new Response(content, {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
}
return new Response('Not found', { status: 404 });
}
return originalFetch(input, init);
};
const originalAllowLocal = env.allowLocalModels;
env.allowLocalModels = false;
try {
const tokenizer = await AutoTokenizer.from_pretrained(fakeModelId);
console.log('Tokenizer created successfully');
return { tokenizer, specialTokens };
} finally {
globalThis.fetch = originalFetch;
env.allowLocalModels = originalAllowLocal;
}
}
export class AudioModel {
constructor() {
this.tokenizer = null;
this.decoderSession = null;
this.audioEncoderSession = null;
this.audioEmbeddingSession = null;
this.audioEmbeddingWeight = null; // Direct lookup (faster than ONNX)
this.audioDetokenizerSession = null;
this.vocoderSession = null;
this.config = null;
this.embedTokensWeight = null;
// Model config
this.hiddenSize = 2048;
this.numLayers = 16;
this.numKVHeads = 8;
this.headDim = 64;
this.convL = 3;
this.layerTypes = [];
this.vocabSize = 65536;
// === Stateful cache for multi-turn conversation ===
this.cache = null;
this.cacheSeqLen = 0;
}
/**
* Reset conversation state (KV cache).
* Call this to start a new conversation.
*/
reset() {
this.cache = null;
this.cacheSeqLen = 0;
log('Conversation state reset');
}
/**
* Load the audio model from a directory
* @param {string} modelPath - Path to model directory
* @param {object} options - Loading options
*/
async load(modelPath, options = {}) {
const { progressCallback, device = 'webgpu', quantization = null } = options;
const report = (status, progress = 0, file = '') => {
if (progressCallback) {
progressCallback({ status, progress, file });
}
};
const executionProviders = device === 'webgpu'
? ['webgpu', 'wasm']
: ['wasm'];
try {
// Load mel config for audio processing
await loadMelConfig(modelPath);
// Load tokenizer
report('loading', 0, 'tokenizer');
const { tokenizer } = await loadTokenizerFromPath(modelPath);
this.tokenizer = tokenizer;
// Load config
report('loading', 5, 'config');
const configResponse = await fetch(`${modelPath}/config.json`, {
mode: 'cors',
credentials: 'omit',
});
this.config = await configResponse.json();
// Extract model dimensions from config
const lfmConfig = this.config.lfm || {};
this.hiddenSize = lfmConfig.hidden_size || 2048;
this.numLayers = lfmConfig.num_hidden_layers || 16;
this.numKVHeads = lfmConfig.num_key_value_heads || 8;
this.headDim = Math.floor(this.hiddenSize / (lfmConfig.num_attention_heads || 32));
this.convL = lfmConfig.conv_L_cache || 3;
this.layerTypes = lfmConfig.layer_types || [];
this.vocabSize = lfmConfig.vocab_size || 65536;
console.log('Model config:', {
hiddenSize: this.hiddenSize,
numLayers: this.numLayers,
numKVHeads: this.numKVHeads,
headDim: this.headDim,
});
// Parse quantization config
const quantConfig = typeof quantization === 'object' ? quantization : {
decoder: quantization,
audioEncoder: quantization,
audioEmbedding: quantization,
audioDetokenizer: quantization,
vocoder: quantization,
};
// Helper to load ONNX model with external data
const loadOnnxWithExternalData = async (name, progress, quantSuffix = null, extraOptions = {}) => {
const suffix = quantSuffix ? `_${quantSuffix}` : '';
const fileName = `${name}${suffix}`;
report('loading', progress, `${fileName}.onnx`);
const onnxPath = `${modelPath}/onnx/${fileName}.onnx`;
const fetchOptions = { mode: 'cors', credentials: 'omit' };
console.log(`Loading ${fileName}...`);
const sessionOptions = { executionProviders, ...extraOptions };
const onnxResponse = await fetchWithCache(onnxPath, fetchOptions);
if (!onnxResponse.ok) {
throw new Error(`Failed to fetch ${fileName}.onnx: ${onnxResponse.status}`);
}
const onnxBuffer = await onnxResponse.arrayBuffer();
console.log(`Loaded ${fileName}.onnx: ${(onnxBuffer.byteLength / 1024 / 1024).toFixed(1)} MB`);
// Load external data files
sessionOptions.externalData = [];
// Try single .onnx_data file
const singleDataPath = `${modelPath}/onnx/${fileName}.onnx_data`;
try {
const dataResponse = await fetchWithCache(singleDataPath, fetchOptions);
const contentType = dataResponse.headers.get('content-type') || '';
if (dataResponse.ok && !contentType.includes('text/html')) {
const dataBuffer = await dataResponse.arrayBuffer();
if (dataBuffer.byteLength > 1000) { // Sanity check
console.log(`Loaded ${fileName}.onnx_data: ${(dataBuffer.byteLength / 1024 / 1024).toFixed(1)} MB`);
sessionOptions.externalData.push({
path: `${fileName}.onnx_data`,
data: new Uint8Array(dataBuffer),
});
}
}
} catch (e) {
// File doesn't exist
}
// Try numbered files - stop on first 404
for (let i = 1; ; i++) {
const numberedDataPath = `${modelPath}/onnx/${fileName}.onnx_data_${i}`;
const dataResponse = await fetch(numberedDataPath, fetchOptions);
if (dataResponse.status === 404 || !dataResponse.ok) break;
const contentType = dataResponse.headers.get('content-type') || '';
if (contentType.includes('text/html')) break;
const dataBuffer = await dataResponse.arrayBuffer();
if (dataBuffer.byteLength < 1000) break;
console.log(`Loaded ${fileName}.onnx_data_${i}: ${(dataBuffer.byteLength / 1024 / 1024).toFixed(1)} MB`);
sessionOptions.externalData.push({
path: `${fileName}.onnx_data_${i}`,
data: new Uint8Array(dataBuffer),
});
}
if (sessionOptions.externalData.length === 0) {
delete sessionOptions.externalData;
}
const session = await ort.InferenceSession.create(new Uint8Array(onnxBuffer), sessionOptions);
console.log(`Session created for ${fileName}`);
return session;
};
// Load decoder
// On WebGPU: keep KV cache outputs on GPU to avoid GPU→CPU→GPU roundtrips between steps
const decoderOpts = device === 'webgpu' ? (() => {
const loc = {};
for (let i = 0; i < this.layerTypes.length; i++) {
if (this.layerTypes[i] === 'conv') {
loc[`present_conv.${i}`] = 'gpu-buffer';
} else {
loc[`present.${i}.key`] = 'gpu-buffer';
loc[`present.${i}.value`] = 'gpu-buffer';
}
}
return { preferredOutputLocation: loc };
})() : {};
this.decoderSession = await loadOnnxWithExternalData('decoder', 10, quantConfig.decoder, decoderOpts);
// Load embed_tokens weight for text embedding lookup
report('loading', 30, 'embed_tokens');
this.embedTokensWeight = await this.loadEmbedTokensWeight(modelPath);
// Load audio encoder (for ASR)
this.audioEncoderSession = await loadOnnxWithExternalData('audio_encoder', 50, quantConfig.audioEncoder);
// Load audio embedding (for TTS) - try binary first, fallback to ONNX
report('loading', 65, 'audio_embedding');
this.audioEmbeddingWeight = await this.loadAudioEmbeddingWeight(modelPath);
if (!this.audioEmbeddingWeight) {
// Fallback to ONNX model
this.audioEmbeddingSession = await loadOnnxWithExternalData('audio_embedding', 70, quantConfig.audioEmbedding);
} else {
console.log('Using direct audio embedding lookup (binary)');
}
// Load audio detokenizer (for TTS output)
try {
this.audioDetokenizerSession = await loadOnnxWithExternalData('audio_detokenizer', 85, quantConfig.audioDetokenizer);
} catch (e) {
console.warn('Audio detokenizer not available:', e);
}
// Load vocoder (for TTS)
// On WebGPU: keep KV cache on GPU to avoid GPU→CPU→GPU roundtrips between steps
try {
const vocoderOpts = device === 'webgpu'
? { preferredOutputLocation: { new_keys: 'gpu-buffer', new_values: 'gpu-buffer', depth_slices: 'gpu-buffer' } }
: {};
this.vocoderSession = await loadOnnxWithExternalData('vocoder_depthformer', 95, quantConfig.vocoder, vocoderOpts);
} catch (e) {
console.warn('Vocoder not available:', e);
}
report('done', 100, '');
return true;
} catch (error) {
let errorMessage = error;
if (typeof error === 'number') {
errorMessage = `ONNX Runtime error code: ${error}`;
} else if (error instanceof Error) {
errorMessage = error.message;
}
console.error('Failed to load audio model:', errorMessage);
throw new Error(errorMessage);
}
}
/**
* Get audio embeddings for given token indices and sum across codebooks
*
* Uses direct array indexing if binary weight available (fast),
* falls back to ONNX session otherwise.
*
* @param {number[]} audioTokens - Array of 8 token indices (one per codebook)
* @returns {Float32Array} Summed embedding [hiddenSize]
*/
async getAudioEmbedding(audioTokens) {
const NUM_CODEBOOKS = 8;
const hiddenSize = this.hiddenSize;
const summedEmbeds = new Float32Array(hiddenSize);
if (this.audioEmbeddingWeight) {
// Direct lookup (much faster - no ONNX call)
const weight = this.audioEmbeddingWeight.weight;
for (let cb = 0; cb < NUM_CODEBOOKS; cb++) {
const tokenIdx = audioTokens[cb];
const offset = tokenIdx * hiddenSize;
for (let h = 0; h < hiddenSize; h++) {
summedEmbeds[h] += weight[offset + h];
}
}
} else {
// Fallback to ONNX session
const audioTokensTensor = new ort.Tensor('int64', new BigInt64Array(audioTokens.map(BigInt)), [1, NUM_CODEBOOKS]);
const result = await this.audioEmbeddingSession.run({ audio_codes: audioTokensTensor });
const embeddings = result.audio_embeds.data;
for (let cb = 0; cb < NUM_CODEBOOKS; cb++) {
for (let h = 0; h < hiddenSize; h++) {
summedEmbeds[h] += embeddings[cb * hiddenSize + h];
}
}
}
return summedEmbeds;
}
/**
* Load audio_embedding.weight from raw binary file for direct lookup
*
* This eliminates ONNX model calls (352 per generation → 0).
* Falls back to ONNX session if binary not available.
*/
async loadAudioEmbeddingWeight(modelPath) {
const fetchOptions = { mode: 'cors', credentials: 'omit' };
try {
// Load metadata
const metaResponse = await fetchWithCache(`${modelPath}/onnx/audio_embedding.json`, fetchOptions);
if (!metaResponse.ok) {
console.log('audio_embedding.json not found, will use ONNX model');
return null;
}
const meta = await metaResponse.json();
console.log('audio_embedding metadata:', meta);
// Load binary weight
const binResponse = await fetchWithCache(`${modelPath}/onnx/audio_embedding.bin`, fetchOptions);
if (!binResponse.ok) {
console.log('audio_embedding.bin not found, will use ONNX model');
return null;
}
const buffer = await binResponse.arrayBuffer();
const weight = new Float32Array(buffer);
if (weight.length !== meta.vocab_size * meta.hidden_size) {
console.error('audio_embedding size mismatch:', weight.length, 'expected:', meta.vocab_size * meta.hidden_size);
return null;
}
console.log(`Loaded audio_embedding: [${meta.vocab_size}, ${meta.hidden_size}] (${(buffer.byteLength / 1e6).toFixed(1)} MB)`);
return { weight, vocabSize: meta.vocab_size, hiddenSize: meta.hidden_size };
} catch (e) {
console.log('Failed to load audio_embedding.bin:', e);
return null;
}
}
/**
* Load embed_tokens.weight from raw binary file for text embedding lookup
*
* The Python export saves embed_tokens.weight as:
* - embed_tokens.bin: raw float32 binary (vocab_size * hidden_size * 4 bytes)
* - embed_tokens.json: metadata (vocab_size, hidden_size)
*/
async loadEmbedTokensWeight(modelPath) {
const fetchOptions = { mode: 'cors', credentials: 'omit' };
// Load metadata
const metaResponse = await fetchWithCache(`${modelPath}/onnx/embed_tokens.json`, fetchOptions);
if (!metaResponse.ok) {
console.warn('embed_tokens.json not found, TTS will be unavailable');
return null;
}
const meta = await metaResponse.json();
console.log('embed_tokens metadata:', meta);
// Load binary weight
const binResponse = await fetchWithCache(`${modelPath}/onnx/embed_tokens.bin`, fetchOptions);
if (!binResponse.ok) {
console.warn('embed_tokens.bin not found, TTS will be unavailable');
return null;
}
const buffer = await binResponse.arrayBuffer();
const weight = new Float32Array(buffer);
if (weight.length !== meta.vocab_size * meta.hidden_size) {
console.error('embed_tokens size mismatch:', weight.length, 'expected:', meta.vocab_size * meta.hidden_size);
return null;
}
console.log(`Loaded embed_tokens: [${meta.vocab_size}, ${meta.hidden_size}] (${(buffer.byteLength / 1e6).toFixed(1)} MB)`);
return { weight, vocabSize: meta.vocab_size, hiddenSize: meta.hidden_size };
}
/**
* Get text embeddings for token IDs using pre-loaded weight
* @param {number[]} tokenIds - Array of token IDs
* @returns {ort.Tensor} - Embeddings tensor [1, seq_len, hidden_size]
*/
getTextEmbeddings(tokenIds) {
if (!this.embedTokensWeight) {
throw new Error('embed_tokens weight not loaded');
}
const { weight, hiddenSize } = this.embedTokensWeight;
const seqLen = tokenIds.length;
const embeddings = new Float32Array(seqLen * hiddenSize);
for (let i = 0; i < seqLen; i++) {
const tokenId = tokenIds[i];
const srcOffset = tokenId * hiddenSize;
const dstOffset = i * hiddenSize;
embeddings.set(weight.subarray(srcOffset, srcOffset + hiddenSize), dstOffset);
}
return new ort.Tensor('float32', embeddings, [1, seqLen, hiddenSize]);
}
/**
* Initialize KV cache for generation
*/
initializeCache() {
const cache = {};
for (let idx = 0; idx < this.layerTypes.length; idx++) {
const layerType = this.layerTypes[idx];
if (layerType === 'conv') {
cache[`past_conv.${idx}`] = new ort.Tensor(
'float32',
new Float32Array(1 * this.hiddenSize * this.convL),
[1, this.hiddenSize, this.convL]
);
} else {
cache[`past_key_values.${idx}.key`] = new ort.Tensor(
'float32',
new Float32Array(0),
[1, this.numKVHeads, 0, this.headDim]
);
cache[`past_key_values.${idx}.value`] = new ort.Tensor(
'float32',
new Float32Array(0),
[1, this.numKVHeads, 0, this.headDim]
);
}
}
return cache;
}
/**
* Update cache from decoder outputs
*/
updateCache(cache, outputs) {
for (const name of Object.keys(outputs)) {
if (name.startsWith('present_conv.')) {
const cacheName = name.replace('present_conv', 'past_conv');
if (cacheName in cache) {
cache[cacheName] = outputs[name];
}
} else if (name.startsWith('present.')) {
const cacheName = name.replace('present.', 'past_key_values.');
if (cacheName in cache) {
cache[cacheName] = outputs[name];
}
}
}
}
/**
* Run decoder with embeddings
*/
async runDecoder(embeds, attentionMask, cache) {
const feeds = {
inputs_embeds: embeds,
attention_mask: attentionMask,
...cache,
};
const outputs = await this.decoderSession.run(feeds);
return {
logits: outputs.logits,
hiddenStates: outputs.hidden_states,
outputs,
};
}
/**
* Sample next token
*/
sampleToken(logits, temperature = 0.7) {
if (temperature === 0) {
let maxIdx = 0;
let maxVal = logits[0];
for (let i = 1; i < logits.length; i++) {
if (logits[i] > maxVal) {
maxVal = logits[i];
maxIdx = i;
}
}
return maxIdx;
}
// Temperature sampling
const scaledLogits = new Float32Array(logits.length);
let maxLogit = -Infinity;
for (let i = 0; i < logits.length; i++) {
scaledLogits[i] = logits[i] / temperature;
maxLogit = Math.max(maxLogit, scaledLogits[i]);
}
let sumExp = 0;
for (let i = 0; i < scaledLogits.length; i++) {
scaledLogits[i] = Math.exp(scaledLogits[i] - maxLogit);
sumExp += scaledLogits[i];
}
const probs = new Float32Array(scaledLogits.length);
for (let i = 0; i < probs.length; i++) {
probs[i] = scaledLogits[i] / sumExp;
}
// Sample from distribution
const r = Math.random();
let cumsum = 0;
for (let i = 0; i < probs.length; i++) {
cumsum += probs[i];
if (r < cumsum) return i;
}
return probs.length - 1;
}
/**
* Transcribe audio to text (ASR mode)
* @param {Float32Array} audioData - Audio samples in [-1, 1]
* @param {number} sampleRate - Audio sample rate
* @param {object} options - Generation options
*/
async transcribe(audioData, sampleRate, options = {}) {
const {
maxNewTokens = DEFAULT_MAX_TOKENS_TEXT,
temperature = 0,
systemPrompt = DEFAULT_SYSTEM_PROMPT_ASR,
onToken,
} = options;
if (!this.audioEncoderSession) {
throw new Error('Audio encoder not loaded');
}
if (!this.embedTokensWeight) {
throw new Error('embed_tokens not loaded - required for ASR');
}
logReset();
log('=== ASR Transcription ===');
log('Audio samples:', audioData.length, 'Sample rate:', sampleRate);
// 1. Compute mel spectrogram
const { melFeatures, numFrames } = computeMelSpectrogram(audioData, sampleRate);
log('Mel spectrogram frames:', numFrames);
// 2. Run audio encoder
const melTensor = new ort.Tensor(
'float32',
melFeatures,
[1, numFrames, 128] // [batch, time, n_mels]
);
const melLengths = new ort.Tensor(
'int64',
new BigInt64Array([BigInt(numFrames)]),
[1]
);
const encoderOutputs = await this.audioEncoderSession.run({
mel_spectrogram: melTensor,
mel_lengths: melLengths,
});
const audioEmbeds = encoderOutputs.audio_embeddings;
log('Audio embeddings shape:', audioEmbeds.dims);
// 3. Build prompt: prefix + audio + suffix
const prefixText = `<|startoftext|><|im_start|>system\n${systemPrompt}<|im_end|>\n<|im_start|>user\n`;
const suffixText = '<|im_end|>\n<|im_start|>assistant\n';
// Use add_special_tokens: false to match Python behavior (prompt already has special tokens)
const prefixIds = Array.from(this.tokenizer.encode(prefixText, { add_special_tokens: false }));
const suffixIds = Array.from(this.tokenizer.encode(suffixText, { add_special_tokens: false }));
log('Prefix tokens:', prefixIds.length, 'Suffix tokens:', suffixIds.length);
// Get text embeddings
const prefixEmbeds = this.getTextEmbeddings(prefixIds);
const suffixEmbeds = this.getTextEmbeddings(suffixIds);
// 4. Concatenate embeddings: prefix + audio + suffix
const prefixLen = prefixIds.length;
const audioLen = audioEmbeds.dims[1];
const suffixLen = suffixIds.length;
const totalLen = prefixLen + audioLen + suffixLen;
const { hiddenSize } = this.embedTokensWeight;
const allEmbeds = new Float32Array(totalLen * hiddenSize);
// Copy prefix embeddings
allEmbeds.set(prefixEmbeds.data, 0);
// Copy audio embeddings
allEmbeds.set(new Float32Array(audioEmbeds.data.buffer, audioEmbeds.data.byteOffset, audioLen * hiddenSize), prefixLen * hiddenSize);
// Copy suffix embeddings
allEmbeds.set(suffixEmbeds.data, (prefixLen + audioLen) * hiddenSize);
const inputEmbeds = new ort.Tensor('float32', allEmbeds, [1, totalLen, hiddenSize]);
const attentionMask = new ort.Tensor('int64', new BigInt64Array(totalLen).fill(1n), [1, totalLen]);
// 5. Initialize cache and run prefill
const cache = this.initializeCache();
let { logits, hiddenStates, outputs } = await this.runDecoder(inputEmbeds, attentionMask, cache);
this.updateCache(cache, outputs);
// 6. Generate tokens
const generatedTokens = [];
let currentLen = totalLen;
for (let i = 0; i < maxNewTokens; i++) {
// Get logits for last position - shape is [1, seq_len, vocab_size]
const logitsData = logits.data;
const seqLen = logits.dims[1];
const lastLogits = new Float32Array(this.vocabSize);
const offset = (seqLen - 1) * this.vocabSize;
for (let j = 0; j < this.vocabSize; j++) {
lastLogits[j] = logitsData[offset + j];
}
const nextToken = this.sampleToken(lastLogits, temperature);
// Check for stop tokens
if (nextToken === this.tokenizer.eos_token_id || nextToken === SPECIAL_TOKENS.IM_END) {
log('Stop token reached');
break;
}
generatedTokens.push(nextToken);
if (onToken) {
const text = this.tokenizer.decode(generatedTokens);
onToken(text, nextToken);
}
// Get embedding for next token
const nextEmbeds = this.getTextEmbeddings([nextToken]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
// Run decoder with single token
({ logits, hiddenStates, outputs } = await this.runDecoder(nextEmbeds, nextMask, cache));
this.updateCache(cache, outputs);
}
const result = this.tokenizer.decode(generatedTokens);
log(`Generated ${generatedTokens.length} tokens: "${result}"`);
return result;
}
/**
* Generate response from messages
* @param {Array} messages - Chat messages
* @param {object} options - Generation options
*/
async generate(messages, options = {}) {
const { maxNewTokens = 256, onToken, audioData = null, sampleRate = null } = options;
// If audio data provided, do ASR
if (audioData && sampleRate) {
return this.transcribe(audioData, sampleRate, {
maxNewTokens,
onToken,
});
}
// Text-only generation (simplified)
const prompt = this.tokenizer.apply_chat_template(messages, {
add_generation_prompt: true,
tokenize: false,
});
const inputIds = this.tokenizer.encode(prompt);
console.log('Input tokens:', inputIds.length);
// Initialize cache
const cache = this.initializeCache();
const generatedTokens = [];
// Note: Full implementation needs proper text embedding support
// This is a placeholder that shows the model is loaded
return '[Text generation requires full embedding support - model loaded successfully]';
}
/**
* Initialize reusable vocoder tensors to reduce allocation overhead
*/
_initVocoderCache() {
if (this._vocoderCache) return;
const numLayers = 6;
const numKvHeads = 8;
const headDim = 32;
// Pre-allocate data arrays
const stepIdxData = new BigInt64Array(1);
const prevTokenData = new BigInt64Array(1);
const seqlensKData = new Int32Array(1);
const totalSeqLenData = new Int32Array(1);
// Pre-allocate tensors that can be reused
this._vocoderCache = {
hiddenTensor: null, // Created per-call since hiddenState changes
stepIdxData,
prevTokenData,
seqlensKData,
totalSeqLenData,
// Pre-create reusable tensors (ONNX Runtime reads from the data array)
stepIdxTensor: new ort.Tensor('int64', stepIdxData, []),
prevTokenTensor: new ort.Tensor('int64', prevTokenData, [1]),
seqlensKTensor: new ort.Tensor('int32', seqlensKData, [1]),
totalSeqLenTensor: new ort.Tensor('int32', totalSeqLenData, []),
emptyKeysData: new Float32Array(0),
emptyValuesData: new Float32Array(0),
emptyDepthSlicesData: new Float32Array(8 * 1024), // zeros for step 0
// Reusable sampling arrays
scaledLogits: new Float32Array(2049), // codebook vocab size
indices: new Uint16Array(2049), // Use typed array for faster reset
probs: new Float32Array(64), // top-k size
};
// Initialize indices
for (let i = 0; i < 2049; i++) {
this._vocoderCache.indices[i] = i;
}
}
/**
* Sample audio codes using vocoder depthformer
* Optimized to reduce tensor creation overhead
* @param {Float32Array} hiddenState - [hidden_size] hidden state
* @param {number} temperature - Sampling temperature
* @param {number} topK - Top-k sampling
* @returns {number[]} - 8 codebook values
*/
async sampleAudioCodes(hiddenState, temperature = 0.8, topK = 64) {
if (!this.vocoderSession) {
throw new Error('Vocoder not loaded');
}
// Initialize cache on first call
this._initVocoderCache();
const cache = this._vocoderCache;
const numCodebooks = 8;
const numLayers = 6;
const numKvHeads = 8;
const headDim = 32;
const codes = [];
let prevToken = 0;
// Create hidden state tensor (must be new since data changes)
const hiddenTensor = new ort.Tensor('float32', hiddenState, [1, this.hiddenSize]);
// Initialize empty KV cache
let pastKeys = new ort.Tensor(
'float32',
cache.emptyKeysData,
[numLayers, 1, numKvHeads, 0, headDim]
);
let pastValues = new ort.Tensor(
'float32',
cache.emptyValuesData,
[numLayers, 1, numKvHeads, 0, headDim]
);
// Reuse step_idx and prev_token tensors by updating their data
cache.stepIdxData[0] = 0n;
cache.prevTokenData[0] = 0n;
// depth_slices_in: zeros at step 0 (model ignores it), then fed back from output
let depthSlicesIn = new ort.Tensor('float32', cache.emptyDepthSlicesData, [1, 8, 1024]);
for (let i = 0; i < numCodebooks; i++) {
// Update mutable tensor data (tensor objects reuse the underlying data arrays)
cache.stepIdxData[0] = BigInt(i);
cache.prevTokenData[0] = BigInt(prevToken);
cache.seqlensKData[0] = i;
cache.totalSeqLenData[0] = i + 1;
const feeds = {
hidden_states: hiddenTensor,
depth_slices_in: depthSlicesIn,
step_idx: cache.stepIdxTensor,
prev_token: cache.prevTokenTensor,
past_keys: pastKeys,
past_values: pastValues,
seqlens_k: cache.seqlensKTensor,
total_seq_len: cache.totalSeqLenTensor,
};
const outputs = await this.vocoderSession.run(feeds);
depthSlicesIn = outputs.depth_slices;
const logits = outputs.logits.data;
const vocabSize = logits.length;
// Sample with temperature and top-k (reusing cached arrays)
let token;
if (temperature <= 0) {
// Greedy
token = 0;
let maxVal = logits[0];
for (let j = 1; j < vocabSize; j++) {
if (logits[j] > maxVal) {
maxVal = logits[j];
token = j;
}
}
} else {
// Top-k sampling with reused arrays
const scaledLogits = cache.scaledLogits;
const indices = cache.indices;
const probs = cache.probs;
// Scale logits by temperature and find top-k in single pass
// Use partial selection sort (O(k*n) which is fast for small k)
for (let j = 0; j < vocabSize; j++) {
scaledLogits[j] = logits[j] / temperature;
indices[j] = j;
}
// Partial sort to get top-k
for (let j = 0; j < topK; j++) {
let maxIdx = j;
for (let k = j + 1; k < vocabSize; k++) {
if (scaledLogits[indices[k]] > scaledLogits[indices[maxIdx]]) {
maxIdx = k;
}
}
// Swap
const tmp = indices[j];
indices[j] = indices[maxIdx];
indices[maxIdx] = tmp;
}
// Softmax over top-k
const maxLogit = scaledLogits[indices[0]];
let sumExp = 0;
for (let j = 0; j < topK; j++) {
probs[j] = Math.exp(scaledLogits[indices[j]] - maxLogit);
sumExp += probs[j];
}
for (let j = 0; j < topK; j++) {
probs[j] /= sumExp;
}
// Sample
const r = Math.random();
let cumsum = 0;
token = indices[topK - 1]; // Default to last
for (let j = 0; j < topK; j++) {
cumsum += probs[j];
if (r < cumsum) {
token = indices[j];
break;
}
}
}
codes.push(token);
prevToken = token;
// Update KV cache
pastKeys = outputs.new_keys;
pastValues = outputs.new_values;
}
return codes;
}
/**
* Generate speech from text (TTS mode)
* @param {string} text - Text to convert to speech
* @param {object} options - Generation options
* @returns {object} - { audioCodes: number[][], textTokens: number[] }
*/
async generateSpeech(text, options = {}) {
const {
maxNewTokens = DEFAULT_MAX_TOKENS_AUDIO,
textTemperature = 0.7,
audioTemperature = 0.8,
audioTopK = 64,
systemPrompt = DEFAULT_SYSTEM_PROMPT_TTS,
onToken,
onAudioFrame,
} = options;
logReset();
log('=== TTS Generation ===');
log('Text:', text);
if (!this.embedTokensWeight) {
throw new Error('embed_tokens not loaded - required for TTS');
}
if (!this.vocoderSession) {
throw new Error('Vocoder not loaded - required for TTS');
}
// Build TTS prompt
const prompt = `<|startoftext|><|im_start|>system\n${systemPrompt}<|im_end|>\n<|im_start|>user\n${text}<|im_end|>\n<|im_start|>assistant\n`;
// Use add_special_tokens: false to match Python behavior (prompt already has special tokens)
const inputIds = Array.from(this.tokenizer.encode(prompt, { add_special_tokens: false }));
log('TTS prompt tokens:', inputIds.length);
// Get embeddings and run prefill
const inputEmbeds = this.getTextEmbeddings(inputIds);
const cache = this.initializeCache();
const attentionMask = new ort.Tensor('int64', new BigInt64Array(inputIds.length).fill(1n), [1, inputIds.length]);
let { logits, hiddenStates, outputs } = await this.runDecoder(inputEmbeds, attentionMask, cache);
this.updateCache(cache, outputs);
// Phase 1: Generate text until <|audio_start|> token
const textTokens = [];
let currentLen = inputIds.length;
let inAudioMode = false;
let tokensGenerated = 0;
while (tokensGenerated < maxNewTokens && !inAudioMode) {
const logitsData = logits.data;
const seqLen = logits.dims[1];
// Get logits for last position - shape is [1, seq_len, vocab_size]
const lastLogits = new Float32Array(this.vocabSize);
const offset = (seqLen - 1) * this.vocabSize;
for (let i = 0; i < this.vocabSize; i++) {
lastLogits[i] = logitsData[offset + i];
}
const nextToken = this.sampleToken(lastLogits, textTemperature);
tokensGenerated++;
if (nextToken === this.tokenizer.eos_token_id) {
console.warn('Model produced EOS before audio, TTS may not work');
break;
}
if (nextToken === SPECIAL_TOKENS.AUDIO_START) {
log('Model entered audio mode');
inAudioMode = true;
// Feed audio_start token to get hidden states for first audio frame
const nextEmbeds = this.getTextEmbeddings([SPECIAL_TOKENS.AUDIO_START]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
({ logits, hiddenStates, outputs } = await this.runDecoder(nextEmbeds, nextMask, cache));
this.updateCache(cache, outputs);
break;
}
textTokens.push(nextToken);
// Continue text generation
const nextEmbeds = this.getTextEmbeddings([nextToken]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
({ logits, hiddenStates, outputs } = await this.runDecoder(nextEmbeds, nextMask, cache));
this.updateCache(cache, outputs);
}
if (!inAudioMode) {
console.warn('Model did not enter audio mode, forcing audio generation');
// Force audio start token
const nextEmbeds = this.getTextEmbeddings([SPECIAL_TOKENS.AUDIO_START]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
({ logits, hiddenStates, outputs } = await this.runDecoder(nextEmbeds, nextMask, cache));
this.updateCache(cache, outputs);
tokensGenerated++;
}
// Phase 2: Generate audio frames using depthformer
const audioCodes = [];
const startTime = performance.now();
while (tokensGenerated < maxNewTokens) {
// Get hidden state for last position
const hiddenData = hiddenStates.data;
const seqLen = hiddenStates.dims[1];
const lastHidden = hiddenData.slice((seqLen - 1) * this.hiddenSize, seqLen * this.hiddenSize);
// Sample audio codes
const frameCodes = await this.sampleAudioCodes(lastHidden, audioTemperature, audioTopK);
// Check for end-of-audio
// Only check first codebook (matching liquid-audio TTS behavior)
if (frameCodes[0] >= END_OF_AUDIO_TOKEN) {
log(`End of audio at frame ${audioCodes.length}`);
break;
}
// Log progress periodically
if (audioCodes.length % 50 === 0) {
log(`Generated ${audioCodes.length} audio frames`);
}
audioCodes.push(frameCodes);
tokensGenerated++;
if (onAudioFrame) {
onAudioFrame(frameCodes, audioCodes.length);
}
// Feed back audio codes to continue generation
// Audio embedding expects tokens in range [0, 16392) where:
// token = codebook_idx * 2049 + code_value
const clampedCodes = frameCodes.map(c => Math.min(c, 2047));
const audioTokens = clampedCodes.map((code, idx) => idx * CODEBOOK_VOCAB + code);
// Get summed embeddings for all 8 codebooks
const summedEmbeds = await this.getAudioEmbedding(audioTokens);
const nextEmbeds = new ort.Tensor('float32', summedEmbeds, [1, 1, this.hiddenSize]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
({ logits, hiddenStates, outputs } = await this.runDecoder(nextEmbeds, nextMask, cache));
this.updateCache(cache, outputs);
}
const elapsed = (performance.now() - startTime) / 1000;
const framesPerSec = audioCodes.length / elapsed;
log(`Generated ${audioCodes.length} audio frames in ${elapsed.toFixed(2)}s (${framesPerSec.toFixed(1)} frames/s)`);
const textOutput = textTokens.length > 0 ? this.tokenizer.decode(textTokens) : '';
return { audioCodes, textTokens, textOutput };
}
/**
* Generate interleaved response (mixed text/audio) with stateful KV cache.
*
* The cache is preserved between calls for multi-turn conversation.
* Call reset() to start a new conversation.
*
* @param {Float32Array} audioData - Input audio samples
* @param {number} sampleRate - Audio sample rate
* @param {string} textPrompt - Optional text prompt (unused, for API compatibility)
* @param {object} options - Generation options
* @returns {object} - { text: string, audioCodes: number[][] }
*/
async generateInterleaved(audioData, sampleRate, textPrompt = '', options = {}) {
const {
maxNewTokens = DEFAULT_MAX_TOKENS_AUDIO,
textTemperature = 1.0,
audioTemperature = 1.0,
audioTopK = 4,
systemPrompt = DEFAULT_SYSTEM_PROMPT_INTERLEAVED,
onToken,
onAudioFrame,
} = options;
// Counter-based mode switching (matching liquid-audio)
const INTERLEAVED_N_TEXT = 6;
const INTERLEAVED_N_AUDIO = 12;
logReset();
log('=== Interleaved Generation ===');
log('Cache state:', this.cache ? `exists (seq_len=${this.cacheSeqLen})` : 'null (new conversation)');
log('Audio samples:', audioData.length, 'Sample rate:', sampleRate);
if (!this.audioEncoderSession) {
throw new Error('Audio encoder not loaded - required for interleaved mode');
}
if (!this.embedTokensWeight) {
throw new Error('embed_tokens not loaded - required for interleaved mode');
}
if (!this.vocoderSession) {
throw new Error('Vocoder not loaded - required for interleaved mode');
}
// Timing accumulators
let timeAudioEncode = 0;
let timePrefill = 0;
let timeTextDecode = 0;
let timeAudioDecode = 0;
let timeVocoder = 0;
let timeAudioEmbed = 0;
// 1. Compute mel spectrogram and encode audio
let tStep = performance.now();
const { melFeatures, numFrames } = computeMelSpectrogram(audioData, sampleRate);
const timeMel = performance.now() - tStep;
const melTensor = new ort.Tensor('float32', melFeatures, [1, numFrames, 128]);
const melLengths = new ort.Tensor('int64', new BigInt64Array([BigInt(numFrames)]), [1]);
tStep = performance.now();
const encoderOutputs = await this.audioEncoderSession.run({
mel_spectrogram: melTensor,
mel_lengths: melLengths,
});
timeAudioEncode = performance.now() - tStep;
const audioEmbeds = encoderOutputs.audio_embeddings;
log(`Mel: ${timeMel.toFixed(0)}ms, AudioEnc: ${timeAudioEncode.toFixed(0)}ms, frames: ${numFrames}`);
const { hiddenSize } = this.embedTokensWeight;
// 2. Build prompt based on whether this is first turn or continuation
let inputEmbeds;
let newSeqLen;
if (this.cache === null) {
// === First turn: full prompt with system message ===
log('First turn - initializing conversation');
this.cache = this.initializeCache();
this.cacheSeqLen = 0;
const prefixText = `<|startoftext|><|im_start|>system\n${systemPrompt}<|im_end|>\n<|im_start|>user\n`;
const suffixText = '<|im_end|>\n<|im_start|>assistant\n';
const prefixIds = Array.from(this.tokenizer.encode(prefixText, { add_special_tokens: false }));
const suffixIds = Array.from(this.tokenizer.encode(suffixText, { add_special_tokens: false }));
const prefixEmbeds = this.getTextEmbeddings(prefixIds);
const suffixEmbeds = this.getTextEmbeddings(suffixIds);
const prefixLen = prefixIds.length;
const audioLen = audioEmbeds.dims[1];
const suffixLen = suffixIds.length;
newSeqLen = prefixLen + audioLen + suffixLen;
const allEmbeds = new Float32Array(newSeqLen * hiddenSize);
allEmbeds.set(prefixEmbeds.data, 0);
allEmbeds.set(
new Float32Array(audioEmbeds.data.buffer, audioEmbeds.data.byteOffset, audioLen * hiddenSize),
prefixLen * hiddenSize
);
allEmbeds.set(suffixEmbeds.data, (prefixLen + audioLen) * hiddenSize);
inputEmbeds = new ort.Tensor('float32', allEmbeds, [1, newSeqLen, hiddenSize]);
} else {
// === Continuation: user turn only ===
log(`Continuing conversation (cache seq_len=${this.cacheSeqLen})`);
const userPrefixText = '<|im_start|>user\n';
const suffixText = '<|im_end|>\n<|im_start|>assistant\n';
const userPrefixIds = Array.from(this.tokenizer.encode(userPrefixText, { add_special_tokens: false }));
const suffixIds = Array.from(this.tokenizer.encode(suffixText, { add_special_tokens: false }));
const userPrefixEmbeds = this.getTextEmbeddings(userPrefixIds);
const suffixEmbeds = this.getTextEmbeddings(suffixIds);
const userPrefixLen = userPrefixIds.length;
const audioLen = audioEmbeds.dims[1];
const suffixLen = suffixIds.length;
newSeqLen = userPrefixLen + audioLen + suffixLen;
const allEmbeds = new Float32Array(newSeqLen * hiddenSize);
allEmbeds.set(userPrefixEmbeds.data, 0);
allEmbeds.set(
new Float32Array(audioEmbeds.data.buffer, audioEmbeds.data.byteOffset, audioLen * hiddenSize),
userPrefixLen * hiddenSize
);
allEmbeds.set(suffixEmbeds.data, (userPrefixLen + audioLen) * hiddenSize);
inputEmbeds = new ort.Tensor('float32', allEmbeds, [1, newSeqLen, hiddenSize]);
}
// 3. Run prefill with attention mask covering full sequence
const totalLen = this.cacheSeqLen + newSeqLen;
const attentionMask = new ort.Tensor('int64', new BigInt64Array(totalLen).fill(1n), [1, totalLen]);
tStep = performance.now();
let { logits, hiddenStates, outputs } = await this.runDecoder(inputEmbeds, attentionMask, this.cache);
timePrefill = performance.now() - tStep;
this.updateCache(this.cache, outputs);
this.cacheSeqLen = totalLen;
log(`Prefill: ${timePrefill.toFixed(0)}ms, new tokens: ${newSeqLen}, total: ${totalLen}`);
// 4. Generate with counter-based mode switching
const textTokens = [];
const audioCodes = [];
let currentLen = totalLen;
let inAudioMode = false;
let modalityLeft = INTERLEAVED_N_TEXT;
let textDone = false;
const startTime = performance.now();
for (let step = 0; step < maxNewTokens; step++) {
modalityLeft--;
if (inAudioMode) {
// Generate audio frame using depthformer
const hiddenData = hiddenStates.data;
const seqLen = hiddenStates.dims[1];
const lastHidden = hiddenData.slice((seqLen - 1) * hiddenSize, seqLen * hiddenSize);
tStep = performance.now();
const frameCodes = await this.sampleAudioCodes(lastHidden, audioTemperature, audioTopK);
timeVocoder += performance.now() - tStep;
// Switch back to text after N audio frames (if text not done)
if (modalityLeft <= 0 && !textDone) {
inAudioMode = false;
modalityLeft = INTERLEAVED_N_TEXT;
}
// Check for end of audio - first codebook == 2048 (matching liquid-audio)
if (frameCodes[0] === END_OF_AUDIO_TOKEN) {
log(`End of audio at step ${step}`);
// Set all codes to 2048 (matching liquid-audio)
for (let i = 0; i < NUM_CODEBOOKS; i++) {
frameCodes[i] = END_OF_AUDIO_TOKEN;
}
inAudioMode = false;
// Don't save this frame, but still feed it back
} else {
// Save valid frame (clamped to 0-2047)
const clampedFrame = frameCodes.map(c => Math.min(c, 2047));
audioCodes.push(clampedFrame);
if (onAudioFrame) {
onAudioFrame(clampedFrame, audioCodes.length);
}
if (audioCodes.length % 50 === 0) {
log(`Generated ${audioCodes.length} audio frames`);
}
}
// Get embeddings for next step (always feed back, even for 2048 frames)
tStep = performance.now();
const feedCodes = frameCodes.map(c => c === END_OF_AUDIO_TOKEN ? END_OF_AUDIO_TOKEN : Math.min(c, 2047));
const audioTokens = feedCodes.map((code, idx) => idx * CODEBOOK_VOCAB + code);
// Get summed embeddings for all 8 codebooks
const summedEmbeds = await this.getAudioEmbedding(audioTokens);
timeAudioEmbed += performance.now() - tStep;
const nextEmbeds = new ort.Tensor('float32', summedEmbeds, [1, 1, hiddenSize]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
tStep = performance.now();
({ logits, hiddenStates, outputs } = await this.runDecoder(nextEmbeds, nextMask, this.cache));
timeAudioDecode += performance.now() - tStep;
this.updateCache(this.cache, outputs);
} else {
// Generate text token
const logitsData = logits.data;
const seqLen = logits.dims[1];
// Get logits for last position - shape is [1, seq_len, vocab_size]
const lastLogits = new Float32Array(this.vocabSize);
const offset = (seqLen - 1) * this.vocabSize;
for (let i = 0; i < this.vocabSize; i++) {
lastLogits[i] = logitsData[offset + i];
}
const token = this.sampleToken(lastLogits, textTemperature);
// Check for end of turn
if (token === this.tokenizer.eos_token_id || token === SPECIAL_TOKENS.IM_END) {
log(`End of turn at step ${step}`);
break;
}
// Check for <|text_end|> token (130)
if (token === SPECIAL_TOKENS.TEXT_END) {
log(`Text end at step ${step}`);
textDone = true;
}
// Switch to audio after N text tokens OR text_end
if (modalityLeft <= 0 || textDone) {
inAudioMode = true;
modalityLeft = INTERLEAVED_N_AUDIO;
}
textTokens.push(token);
if (onToken) {
const decodedText = this.tokenizer.decode(textTokens, { skip_special_tokens: true });
onToken(decodedText, token);
}
// Get embedding for next step
const nextEmbeds = this.getTextEmbeddings([token]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
tStep = performance.now();
({ logits, hiddenStates, outputs } = await this.runDecoder(nextEmbeds, nextMask, this.cache));
timeTextDecode += performance.now() - tStep;
this.updateCache(this.cache, outputs);
}
}
// 5. Feed <|im_end|> token to close assistant turn in cache
const imEndEmbeds = this.getTextEmbeddings([SPECIAL_TOKENS.IM_END]);
currentLen++;
const finalMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
({ outputs } = await this.runDecoder(imEndEmbeds, finalMask, this.cache));
this.updateCache(this.cache, outputs);
this.cacheSeqLen = currentLen;
// Decode with skip_special_tokens to clean up special tokens like <|text_end|>
const text = this.tokenizer.decode(textTokens, { skip_special_tokens: true });
// Print timing summary
log(`=== Summary ===`);
log(` Mel: ${timeMel.toFixed(0)}ms, AudioEnc: ${timeAudioEncode.toFixed(0)}ms, Prefill: ${timePrefill.toFixed(0)}ms`);
log(` TextDec: ${timeTextDecode.toFixed(0)}ms (${textTokens.length} tok), AudioDec: ${timeAudioDecode.toFixed(0)}ms`);
log(` Vocoder: ${timeVocoder.toFixed(0)}ms, AudioEmbed: ${timeAudioEmbed.toFixed(0)}ms`);
log(`Output: ${textTokens.length} text tokens, ${audioCodes.length} audio frames`);
log(`Text: "${text}"`);
log(`Cache seq_len: ${this.cacheSeqLen}`);
return { text, audioCodes };
}
/**
* Generate interleaved response from text-only input (continuation turn).
* Uses the stateful KV cache from previous turns. Produces both text AND audio.
*
* @param {string} userText - User's text message
* @param {object} options - Generation options
* @returns {object} - { text: string, audioCodes: number[][] }
*/
async generateInterleavedFromText(userText, options = {}) {
const {
maxNewTokens = DEFAULT_MAX_TOKENS_AUDIO,
textTemperature = 1.0,
audioTemperature = 1.0,
audioTopK = 4,
systemPrompt = DEFAULT_SYSTEM_PROMPT_INTERLEAVED,
onToken,
onAudioFrame,
} = options;
// Counter-based mode switching (matching liquid-audio)
const INTERLEAVED_N_TEXT = 6;
const INTERLEAVED_N_AUDIO = 12;
logReset();
log('=== Text-Only Interleaved Generation ===');
log(`Cache state: ${this.cache ? `exists (seq_len=${this.cacheSeqLen})` : 'null (new conversation)'}`);
log(`User text: ${userText}`);
if (!this.embedTokensWeight) {
throw new Error('embed_tokens not loaded - required for text generation');
}
if (!this.vocoderSession) {
throw new Error('Vocoder not loaded - required for interleaved mode');
}
// Timing accumulators
let timePrefill = 0;
let timeTextDecode = 0;
let timeAudioDecode = 0;
let timeVocoder = 0;
let timeAudioEmbed = 0;
let tStep;
const { hiddenSize } = this.embedTokensWeight;
// Build prompt based on whether this is first turn or continuation
let inputEmbeds;
let newSeqLen;
if (this.cache === null) {
// === First turn: full prompt with system message ===
log('First turn - initializing conversation');
this.cache = this.initializeCache();
this.cacheSeqLen = 0;
const prefixText = `<|startoftext|><|im_start|>system\n${systemPrompt}<|im_end|>\n<|im_start|>user\n${userText}<|im_end|>\n<|im_start|>assistant\n`;
const prefixIds = Array.from(this.tokenizer.encode(prefixText, { add_special_tokens: false }));
const prefixEmbeds = this.getTextEmbeddings(prefixIds);
newSeqLen = prefixIds.length;
inputEmbeds = new ort.Tensor('float32', prefixEmbeds.data, [1, newSeqLen, hiddenSize]);
} else {
// === Continuation: user turn only ===
log(`Continuing conversation (cache seq_len=${this.cacheSeqLen})`);
const userTurnText = `<|im_start|>user\n${userText}<|im_end|>\n<|im_start|>assistant\n`;
const userTurnIds = Array.from(this.tokenizer.encode(userTurnText, { add_special_tokens: false }));
const userTurnEmbeds = this.getTextEmbeddings(userTurnIds);
newSeqLen = userTurnIds.length;
inputEmbeds = new ort.Tensor('float32', userTurnEmbeds.data, [1, newSeqLen, hiddenSize]);
}
// Run prefill with attention mask covering full sequence
const totalLen = this.cacheSeqLen + newSeqLen;
const attentionMask = new ort.Tensor('int64', new BigInt64Array(totalLen).fill(1n), [1, totalLen]);
tStep = performance.now();
let { logits, hiddenStates, outputs } = await this.runDecoder(inputEmbeds, attentionMask, this.cache);
timePrefill = performance.now() - tStep;
this.updateCache(this.cache, outputs);
this.cacheSeqLen = totalLen;
log(`Prefill: ${timePrefill.toFixed(0)}ms, new tokens: ${newSeqLen}, total: ${totalLen}`);
// Generate with counter-based mode switching
const textTokens = [];
const audioCodes = [];
let currentLen = totalLen;
let inAudioMode = false;
let modalityLeft = INTERLEAVED_N_TEXT;
let textDone = false;
for (let step = 0; step < maxNewTokens; step++) {
modalityLeft--;
if (inAudioMode) {
// Generate audio frame using depthformer
const hiddenData = hiddenStates.data;
const seqLen = hiddenStates.dims[1];
const lastHidden = hiddenData.slice((seqLen - 1) * hiddenSize, seqLen * hiddenSize);
tStep = performance.now();
const frameCodes = await this.sampleAudioCodes(lastHidden, audioTemperature, audioTopK);
timeVocoder += performance.now() - tStep;
// Switch back to text after N audio frames (if text not done)
if (modalityLeft <= 0 && !textDone) {
inAudioMode = false;
modalityLeft = INTERLEAVED_N_TEXT;
}
// Check for end of audio
if (frameCodes[0] === END_OF_AUDIO_TOKEN) {
log(`End of audio at step ${step}`);
for (let i = 0; i < NUM_CODEBOOKS; i++) {
frameCodes[i] = END_OF_AUDIO_TOKEN;
}
inAudioMode = false;
} else {
const clampedFrame = frameCodes.map(c => Math.min(c, 2047));
audioCodes.push(clampedFrame);
if (onAudioFrame) {
onAudioFrame(clampedFrame, audioCodes.length);
}
if (audioCodes.length % 50 === 0) {
log(`Generated ${audioCodes.length} audio frames`);
}
}
// Get embeddings for next step
tStep = performance.now();
const feedCodes = frameCodes.map(c => c === END_OF_AUDIO_TOKEN ? END_OF_AUDIO_TOKEN : Math.min(c, 2047));
const audioTokens = feedCodes.map((code, idx) => idx * CODEBOOK_VOCAB + code);
const summedEmbeds = await this.getAudioEmbedding(audioTokens);
timeAudioEmbed += performance.now() - tStep;
const nextEmbeds = new ort.Tensor('float32', summedEmbeds, [1, 1, hiddenSize]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
tStep = performance.now();
({ logits, hiddenStates, outputs } = await this.runDecoder(nextEmbeds, nextMask, this.cache));
timeAudioDecode += performance.now() - tStep;
this.updateCache(this.cache, outputs);
} else {
// Generate text token
const logitsData = logits.data;
const seqLen = logits.dims[1];
const lastLogits = new Float32Array(this.vocabSize);
const offset = (seqLen - 1) * this.vocabSize;
for (let i = 0; i < this.vocabSize; i++) {
lastLogits[i] = logitsData[offset + i];
}
const token = this.sampleToken(lastLogits, textTemperature);
// Check for end of turn
if (token === this.tokenizer.eos_token_id || token === SPECIAL_TOKENS.IM_END) {
log(`End of turn at step ${step}`);
break;
}
// Check for <|text_end|> token
if (token === SPECIAL_TOKENS.TEXT_END) {
log(`Text end at step ${step}`);
textDone = true;
}
// Switch to audio after N text tokens OR text_end
if (modalityLeft <= 0 || textDone) {
inAudioMode = true;
modalityLeft = INTERLEAVED_N_AUDIO;
}
textTokens.push(token);
if (onToken) {
const decodedText = this.tokenizer.decode(textTokens, { skip_special_tokens: true });
onToken(decodedText, token);
}
// Get embedding for next step
const nextEmbeds = this.getTextEmbeddings([token]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
tStep = performance.now();
({ logits, hiddenStates, outputs } = await this.runDecoder(nextEmbeds, nextMask, this.cache));
timeTextDecode += performance.now() - tStep;
this.updateCache(this.cache, outputs);
}
}
// Feed <|im_end|> token to close assistant turn in cache
const imEndEmbeds = this.getTextEmbeddings([SPECIAL_TOKENS.IM_END]);
currentLen++;
const finalMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
({ outputs } = await this.runDecoder(imEndEmbeds, finalMask, this.cache));
this.updateCache(this.cache, outputs);
this.cacheSeqLen = currentLen;
const text = this.tokenizer.decode(textTokens, { skip_special_tokens: true });
log(`=== Summary ===`);
log(` Prefill: ${timePrefill.toFixed(0)}ms`);
log(` TextDec: ${timeTextDecode.toFixed(0)}ms (${textTokens.length} tok), AudioDec: ${timeAudioDecode.toFixed(0)}ms`);
log(` Vocoder: ${timeVocoder.toFixed(0)}ms, AudioEmbed: ${timeAudioEmbed.toFixed(0)}ms`);
log(`Output: ${textTokens.length} text tokens, ${audioCodes.length} audio frames`);
log(`Text: "${text}"`);
log(`Cache seq_len: ${this.cacheSeqLen}`);
return { text, audioCodes };
}
/**
* Generate text-only response (for follow-up turns without audio).
* Uses the stateful KV cache from previous interleaved turns.
*
* @param {string} userText - User's text input
* @param {object} options - Generation options
* @returns {object} - { text: string }
*/
async generateTextOnly(userText, options = {}) {
const {
maxNewTokens = 256,
temperature = 0.7,
systemPrompt = 'You are a helpful assistant.',
onToken,
} = options;
logReset();
log('=== Text-Only Generation ===');
log('Cache state:', this.cache ? `exists (seq_len=${this.cacheSeqLen})` : 'null (new conversation)');
log('User text:', userText);
if (!this.embedTokensWeight) {
throw new Error('embed_tokens not loaded');
}
const { hiddenSize } = this.embedTokensWeight;
// Build prompt based on whether we have existing cache
let inputEmbeds;
let newSeqLen;
if (this.cache === null) {
// First turn: include system message
log('First turn - initializing conversation');
this.cache = this.initializeCache();
this.cacheSeqLen = 0;
const promptText = `<|startoftext|><|im_start|>system\n${systemPrompt}<|im_end|>\n<|im_start|>user\n${userText}<|im_end|>\n<|im_start|>assistant\n`;
const promptIds = Array.from(this.tokenizer.encode(promptText, { add_special_tokens: false }));
inputEmbeds = this.getTextEmbeddings(promptIds);
newSeqLen = promptIds.length;
} else {
// Continuation: just user turn
log(`Continuing conversation (cache seq_len=${this.cacheSeqLen})`);
const turnText = `<|im_start|>user\n${userText}<|im_end|>\n<|im_start|>assistant\n`;
const turnIds = Array.from(this.tokenizer.encode(turnText, { add_special_tokens: false }));
inputEmbeds = this.getTextEmbeddings(turnIds);
newSeqLen = turnIds.length;
}
// Run prefill
const totalLen = this.cacheSeqLen + newSeqLen;
const attentionMask = new ort.Tensor('int64', new BigInt64Array(totalLen).fill(1n), [1, totalLen]);
let { logits, outputs } = await this.runDecoder(inputEmbeds, attentionMask, this.cache);
this.updateCache(this.cache, outputs);
this.cacheSeqLen = totalLen;
// Generate tokens
const textTokens = [];
let currentLen = totalLen;
for (let i = 0; i < maxNewTokens; i++) {
const logitsData = logits.data;
const seqLen = logits.dims[1];
const lastLogits = new Float32Array(this.vocabSize);
const offset = (seqLen - 1) * this.vocabSize;
for (let j = 0; j < this.vocabSize; j++) {
lastLogits[j] = logitsData[offset + j];
}
const nextToken = this.sampleToken(lastLogits, temperature);
// Check for stop tokens
if (nextToken === this.tokenizer.eos_token_id || nextToken === SPECIAL_TOKENS.IM_END) {
log('Stop token reached');
break;
}
textTokens.push(nextToken);
if (onToken) {
const text = this.tokenizer.decode(textTokens, { skip_special_tokens: true });
onToken(text, nextToken);
}
// Get embedding for next token
const nextEmbeds = this.getTextEmbeddings([nextToken]);
currentLen++;
const nextMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
({ logits, outputs } = await this.runDecoder(nextEmbeds, nextMask, this.cache));
this.updateCache(this.cache, outputs);
}
// Feed <|im_end|> to close turn
const imEndEmbeds = this.getTextEmbeddings([SPECIAL_TOKENS.IM_END]);
currentLen++;
const finalMask = new ort.Tensor('int64', new BigInt64Array(currentLen).fill(1n), [1, currentLen]);
({ outputs } = await this.runDecoder(imEndEmbeds, finalMask, this.cache));
this.updateCache(this.cache, outputs);
this.cacheSeqLen = currentLen;
const text = this.tokenizer.decode(textTokens, { skip_special_tokens: true });
log(`Generated ${textTokens.length} tokens: "${text}"`);
log(`Cache seq_len: ${this.cacheSeqLen}`);
return { text };
}
/**
* Decode audio codes to waveform using audio detokenizer + ISTFT
* @param {number[][]} audioCodes - Array of [8] codebook values per frame
* @returns {Float32Array} - Audio waveform samples in [-1, 1]
*/
async decodeAudioCodes(audioCodes) {
if (!this.audioDetokenizerSession) {
throw new Error('Audio detokenizer not loaded');
}
if (audioCodes.length < 2) {
console.warn('Not enough audio codes to decode');
return new Float32Array(0);
}
const decodeStart = performance.now();
log(`Decoding ${audioCodes.length} audio frames...`);
// ISTFT parameters (fixed for this model)
const nFft = 1280;
const hopLength = 320;
const winLength = 1280;
const nFftBins = nFft / 2 + 1;
// Stack codes: [T, 8] -> [8, T] and add batch -> [1, 8, T]
const T = audioCodes.length;
const codesTransposed = new BigInt64Array(8 * T);
for (let t = 0; t < T; t++) {
for (let cb = 0; cb < 8; cb++) {
codesTransposed[cb * T + t] = BigInt(Math.min(audioCodes[t][cb], 2047));
}
}
// Run detokenizer: [1, 8, T] -> [1, T, 1282]
const codesTensor = new ort.Tensor('int64', codesTransposed, [1, 8, T]);
const detokStart = performance.now();
const detokOutputs = await this.audioDetokenizerSession.run({ audio_codes: codesTensor });
const stftFeatures = detokOutputs.stft_features;
log(`Detokenizer: ${(performance.now() - detokStart).toFixed(0)}ms, STFT frames: ${stftFeatures.dims[1]}`);
// Get raw data - shape is [1, T, 1282], we need to skip batch dimension
const stftData = stftFeatures.data;
const actualT = stftFeatures.dims[1];
// Convert to complex STFT: [log_magnitude | angle] -> complex
const complexStft = new Array(nFftBins);
for (let f = 0; f < nFftBins; f++) {
complexStft[f] = new Array(actualT);
for (let t = 0; t < actualT; t++) {
const logMag = stftData[t * 1282 + f];
const angle = stftData[t * 1282 + nFftBins + f];
const mag = Math.exp(logMag);
// Store as [real, imag]
complexStft[f][t] = [mag * Math.cos(angle), mag * Math.sin(angle)];
}
}
// ISTFT with 'same' padding
const istftStart = performance.now();
const waveform = this.istftSamePadding(complexStft, nFft, hopLength, winLength, actualT);
log(`ISTFT: ${(performance.now() - istftStart).toFixed(0)}ms`);
// Find max/min without spread operator (avoid stack overflow on large arrays)
let waveMax = -Infinity, waveMin = Infinity;
for (let i = 0; i < waveform.length; i++) {
if (waveform[i] > waveMax) waveMax = waveform[i];
if (waveform[i] < waveMin) waveMin = waveform[i];
}
log('ISTFT output - length:', waveform.length, 'max:', waveMax.toFixed(4), 'min:', waveMin.toFixed(4));
// Check for invalid values
if (isNaN(waveMax) || isNaN(waveMin) || !isFinite(waveMax) || !isFinite(waveMin)) {
console.error('ISTFT produced invalid values (NaN/Inf)');
return new Float32Array(0);
}
// Normalize to [-1, 1]
let maxVal = Math.max(Math.abs(waveMax), Math.abs(waveMin));
if (maxVal > 0) {
for (let i = 0; i < waveform.length; i++) {
waveform[i] = (waveform[i] / maxVal) * 0.9;
}
} else {
console.warn('ISTFT produced all-zero waveform');
}
log(`Decoded audio: ${waveform.length} samples (${(waveform.length / 24000).toFixed(2)}s)`);
return waveform;
}
/**
* ISTFT with 'same' padding matching liquid_audio.
* Uses Bluestein FFT for O(N log N) IRFFT on any size.
*
* Matches Python: np.fft.irfft(spec, n_fft, axis=0, norm="backward")
*/
istftSamePadding(complexStft, nFft, hopLength, winLength, T) {
const N = complexStft.length; // nFftBins = nFft/2 + 1 = 641
const pad = Math.floor((winLength - hopLength) / 2);
// Generate Hann window
const window = new Float32Array(winLength);
for (let i = 0; i < winLength; i++) {
window[i] = 0.5 * (1 - Math.cos(2 * Math.PI * i / (winLength - 1)));
}
// Initialize Bluestein FFT for size nFft (cached for reuse)
if (!this._bluesteinCache || this._bluesteinCache.n !== nFft) {
this._bluesteinCache = this._initBluestein(nFft);
}
const bluestein = this._bluesteinCache;
// Pre-allocate buffers for IFFT
const fullRe = new Float32Array(nFft);
const fullIm = new Float32Array(nFft);
// Process all frames
const ifftFrames = new Array(T);
for (let t = 0; t < T; t++) {
// Build full spectrum from one-sided (conjugate symmetry)
fullRe.fill(0);
fullIm.fill(0);
// Copy positive frequencies
for (let k = 0; k < N; k++) {
fullRe[k] = complexStft[k][t][0];
fullIm[k] = complexStft[k][t][1];
}
// Mirror negative frequencies (conjugate symmetry for real signal)
for (let k = 1; k < N - 1; k++) {
fullRe[nFft - k] = fullRe[k];
fullIm[nFft - k] = -fullIm[k];
}
// IFFT using Bluestein: IFFT(X) = conj(FFT(conj(X))) / N
// Conjugate input
for (let i = 0; i < nFft; i++) fullIm[i] = -fullIm[i];
// FFT
this._bluesteinFFT(fullRe, fullIm, bluestein);
// Conjugate and scale
for (let i = 0; i < nFft; i++) {
fullRe[i] /= nFft;
fullIm[i] = -fullIm[i] / nFft;
}
// Apply window (take first winLength samples)
const windowedFrame = new Float32Array(winLength);
for (let n = 0; n < winLength; n++) {
windowedFrame[n] = fullRe[n] * window[n];
}
ifftFrames[t] = windowedFrame;
// Debug first frame
if (t === 0) {
let maxVal = 0;
let hasNaN = false;
for (let n = 0; n < winLength; n++) {
if (isNaN(windowedFrame[n]) || !isFinite(windowedFrame[n])) {
hasNaN = true;
break;
}
const absVal = Math.abs(windowedFrame[n] / (window[n] + 1e-10));
if (absVal > maxVal) maxVal = absVal;
}
if (hasNaN) {
console.error('IRFFT frame 0 contains NaN/Inf values!');
}
}
}
// Overlap-add
const outputSize = (T - 1) * hopLength + winLength;
const audio = new Float32Array(outputSize);
const windowEnvelope = new Float32Array(outputSize);
const windowSq = new Float32Array(winLength);
for (let i = 0; i < winLength; i++) {
windowSq[i] = window[i] * window[i];
}
for (let t = 0; t < T; t++) {
const start = t * hopLength;
for (let n = 0; n < winLength; n++) {
audio[start + n] += ifftFrames[t][n];
windowEnvelope[start + n] += windowSq[n];
}
}
// Normalize and trim padding
const trimmedLength = outputSize - 2 * pad;
const trimmed = new Float32Array(trimmedLength);
for (let i = 0; i < trimmedLength; i++) {
const srcIdx = i + pad;
if (windowEnvelope[srcIdx] > 1e-8) {
trimmed[i] = audio[srcIdx] / windowEnvelope[srcIdx];
} else {
trimmed[i] = audio[srcIdx];
}
}
return trimmed;
}
/**
* Initialize Bluestein FFT for size n (any size, not just power of 2)
*/
_initBluestein(n) {
// Bluestein's algorithm: converts any-size FFT to power-of-2 FFT via convolution
// FFT size for convolution: next power of 2 >= 2n - 1
let m = 1;
while (m < 2 * n - 1) m <<= 1;
// Chirp sequence: W_n^(k^2/2) = exp(-πi * k² / n)
const chirpRe = new Float32Array(n);
const chirpIm = new Float32Array(n);
for (let k = 0; k < n; k++) {
const angle = Math.PI * k * k / n;
chirpRe[k] = Math.cos(angle);
chirpIm[k] = -Math.sin(angle); // exp(-i*angle)
}
// Precompute FFT of chirp filter (b sequence)
// b[k] = conj(chirp[k]) for k in [0, n-1]
// b[m-k] = conj(chirp[k]) for k in [1, n-1]
// conj(chirp[k]) = chirpRe[k] - i*chirpIm[k]
const bRe = new Float32Array(m);
const bIm = new Float32Array(m);
bRe[0] = chirpRe[0];
bIm[0] = -chirpIm[0]; // conjugate
for (let k = 1; k < n; k++) {
bRe[k] = chirpRe[k];
bIm[k] = -chirpIm[k]; // conjugate
bRe[m - k] = chirpRe[k];
bIm[m - k] = -chirpIm[k]; // conjugate
}
// FFT of b (in-place)
this._fftRadix2InPlace(bRe, bIm, m, false);
// Precompute twiddle factors for radix-2 FFT of size m
const twiddleRe = new Float32Array(m / 2);
const twiddleIm = new Float32Array(m / 2);
for (let i = 0; i < m / 2; i++) {
const angle = -2 * Math.PI * i / m;
twiddleRe[i] = Math.cos(angle);
twiddleIm[i] = Math.sin(angle);
}
return { n, m, chirpRe, chirpIm, bRe, bIm, twiddleRe, twiddleIm };
}
/**
* Bluestein FFT for any size
*/
_bluesteinFFT(re, im, cache) {
const { n, m, chirpRe, chirpIm, bRe, bIm, twiddleRe, twiddleIm } = cache;
// a[k] = x[k] * chirp[k] for k in [0, n-1], zero-padded to m
// chirp[k] = chirpRe[k] + i*chirpIm[k]
// (re + i*im) * (chirpRe + i*chirpIm) = (re*chirpRe - im*chirpIm) + i*(im*chirpRe + re*chirpIm)
const aRe = new Float32Array(m);
const aIm = new Float32Array(m);
for (let k = 0; k < n; k++) {
aRe[k] = re[k] * chirpRe[k] - im[k] * chirpIm[k];
aIm[k] = im[k] * chirpRe[k] + re[k] * chirpIm[k];
}
// FFT of a
this._fftRadix2(aRe, aIm, twiddleRe, twiddleIm);
// Pointwise multiply: a = a * b
for (let k = 0; k < m; k++) {
const tmpRe = aRe[k] * bRe[k] - aIm[k] * bIm[k];
const tmpIm = aRe[k] * bIm[k] + aIm[k] * bRe[k];
aRe[k] = tmpRe;
aIm[k] = tmpIm;
}
// IFFT of a (using FFT with conjugate trick)
for (let k = 0; k < m; k++) aIm[k] = -aIm[k];
this._fftRadix2(aRe, aIm, twiddleRe, twiddleIm);
for (let k = 0; k < m; k++) {
aRe[k] /= m;
aIm[k] = -aIm[k] / m;
}
// X[k] = chirp[k] * y[k]
// Same multiplication as for a: (aRe + i*aIm) * (chirpRe + i*chirpIm)
for (let k = 0; k < n; k++) {
re[k] = aRe[k] * chirpRe[k] - aIm[k] * chirpIm[k];
im[k] = aIm[k] * chirpRe[k] + aRe[k] * chirpIm[k];
}
}
/**
* In-place radix-2 FFT (Cooley-Tukey) with precomputed twiddles
*/
_fftRadix2(re, im, twiddleRe, twiddleIm) {
const n = re.length;
// Bit-reversal permutation
for (let i = 0, j = 0; i < n; i++) {
if (i < j) {
let tmp = re[i]; re[i] = re[j]; re[j] = tmp;
tmp = im[i]; im[i] = im[j]; im[j] = tmp;
}
let k = n >> 1;
while (k > 0 && k <= j) { j -= k; k >>= 1; }
j += k;
}
// Cooley-Tukey butterflies
for (let len = 2; len <= n; len <<= 1) {
const halfLen = len >> 1;
const step = n / len;
for (let i = 0; i < n; i += len) {
for (let j = 0; j < halfLen; j++) {
const twIdx = j * step;
const wRe = twiddleRe[twIdx];
const wIm = twiddleIm[twIdx];
const u = i + j;
const v = u + halfLen;
const tRe = wRe * re[v] - wIm * im[v];
const tIm = wRe * im[v] + wIm * re[v];
re[v] = re[u] - tRe;
im[v] = im[u] - tIm;
re[u] += tRe;
im[u] += tIm;
}
}
}
}
/**
* In-place radix-2 FFT without precomputed twiddles (for initialization)
*/
_fftRadix2InPlace(re, im, n, inverse = false) {
// Bit-reversal
for (let i = 0, j = 0; i < n; i++) {
if (i < j) {
let tmp = re[i]; re[i] = re[j]; re[j] = tmp;
tmp = im[i]; im[i] = im[j]; im[j] = tmp;
}
let k = n >> 1;
while (k > 0 && k <= j) { j -= k; k >>= 1; }
j += k;
}
// Butterflies
const sign = inverse ? 1 : -1;
for (let len = 2; len <= n; len <<= 1) {
const halfLen = len >> 1;
const angle = sign * 2 * Math.PI / len;
const wRe = Math.cos(angle);
const wIm = Math.sin(angle);
for (let i = 0; i < n; i += len) {
let curRe = 1, curIm = 0;
for (let j = 0; j < halfLen; j++) {
const u = i + j;
const v = u + halfLen;
const tRe = curRe * re[v] - curIm * im[v];
const tIm = curRe * im[v] + curIm * re[v];
re[v] = re[u] - tRe;
im[v] = im[u] - tIm;
re[u] += tRe;
im[u] += tIm;
const newRe = curRe * wRe - curIm * wIm;
curIm = curRe * wIm + curIm * wRe;
curRe = newRe;
}
}
}
if (inverse) {
for (let i = 0; i < n; i++) {
re[i] /= n;
im[i] /= n;
}
}
}
/**
* Free resources
*/
dispose() {
this.tokenizer = null;
this.decoderSession = null;
this.audioEncoderSession = null;
this.audioEmbeddingSession = null;
this.audioEmbeddingWeight = null;
this.audioDetokenizerSession = null;
this.vocoderSession = null;
this.embedTokensWeight = null;
}
}
// Re-export audio utilities
export { loadAudioFile };
export default AudioModel;
|