File size: 119,219 Bytes
7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 | 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 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 | # Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import os
from abc import ABC, abstractmethod
from typing import Iterable, List, Optional, Tuple
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from transformers import AutoFeatureExtractor, AutoModel, Wav2Vec2BertModel
from nemo.collections.asr.modules import AudioToMelSpectrogramPreprocessor
from nemo.collections.audio.parts.utils.transforms import MelSpectrogram, Resample
from nemo.collections.common.parts.utils import ClampActivation, HalfSnake, Snake, mask_sequence_tensor
from nemo.core.classes.common import typecheck
from nemo.core.classes.module import NeuralModule
from nemo.core.neural_types.elements import (
AudioSignal,
EncodedRepresentation,
LengthsType,
MelSpectrogramType,
TokenIndex,
VoidType,
)
from nemo.core.neural_types.neural_type import NeuralType
from nemo.utils import logging
try:
import fsspec
HAVE_FSSPEC = True
except ModuleNotFoundError:
HAVE_FSSPEC = False
from contextlib import contextmanager
@contextmanager
def default_precision(dtype=torch.float32):
default_dtype = torch.get_default_dtype()
torch.set_default_dtype(dtype)
try:
yield
finally:
torch.set_default_dtype(default_dtype)
def get_padding(kernel_size: int, dilation: int = 1) -> int:
return (kernel_size * dilation - dilation) // 2
def get_padding_2d(kernel_size: Tuple[int, int], dilation: Tuple[int, int]) -> Tuple[int, int]:
paddings = (get_padding(kernel_size[0], dilation[0]), get_padding(kernel_size[1], dilation[1]))
return paddings
def get_down_sample_padding(kernel_size: int, stride: int) -> int:
return (kernel_size - stride + 1) // 2
def get_up_sample_padding(kernel_size: int, stride: int) -> Tuple[int, int]:
output_padding = (kernel_size - stride) % 2
padding = (kernel_size - stride + 1) // 2
return padding, output_padding
class SSLModel(NeuralModule):
def __init__(self, slm_model_name):
super().__init__()
self.ssl_model = AutoModel.from_pretrained(slm_model_name)
def forward(self, *args, **kwargs):
return self.ssl_model(*args, **kwargs)
class SLMDiscriminator(NeuralModule):
"""SLM Discriminator, as described in both the StyleTTS2 and Low Frame-Rate Speech Codec papers.
Args:
slm_model_name: Hugging Face Speech Language Models name.
slm_sr: Speech Language Models input sampling rate.
input_sr: Audio input sampling rate.
slm_hidden: Speech Language Model hidden dim.
slm_layers: Speech Language Model number of layers.
initial_channel: discriminative head number of channels.
use_spectral_norm: If True uses spectral normalization otherwise uses weight norm.
"""
def __init__(
self,
slm_model_name="microsoft/wavlm-base-plus",
slm_sr=16000,
input_sr=22050,
slm_hidden=768,
slm_layers=13,
initial_channel=64,
use_spectral_norm=False,
):
super().__init__()
self.resample = Resample(orig_freq=input_sr, new_freq=slm_sr)
self.slm_model = SSLModel(slm_model_name)
# Freeze slm model
self.slm_model.freeze()
norm_f = (
torch.nn.utils.parametrizations.weight_norm if use_spectral_norm == False else torch.nn.utils.spectral_norm
)
self.pre = norm_f(nn.Conv1d(slm_hidden * slm_layers, initial_channel, 1, 1, padding=0))
self.convs = nn.ModuleList(
[
norm_f(nn.Conv1d(initial_channel, initial_channel * 2, kernel_size=5, padding=2)),
norm_f(nn.Conv1d(initial_channel * 2, initial_channel * 4, kernel_size=5, padding=2)),
norm_f(nn.Conv1d(initial_channel * 4, initial_channel * 4, 5, 1, padding=2)),
]
)
self.conv_post = norm_f(nn.Conv1d(initial_channel * 4, 1, 3, 1, padding=1))
def _forward(self, x):
x = self.slm_model(input_values=self.resample(x), output_hidden_states=True).hidden_states
x = torch.stack(x, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)
x = self.pre(x)
fmap = []
for layer in self.convs:
x = layer(x)
x = F.leaky_relu(x, 0.1)
fmap.append(x.unsqueeze(-1))
x = self.conv_post(x)
x = torch.flatten(x, 1, -1)
return x, fmap
@property
def input_types(self):
return {
"audio_real": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_gen": NeuralType(('B', 'T_audio'), AudioSignal()),
}
@property
def output_types(self):
return {
"scores_real": [NeuralType(('B', 'C', 'T_out'), VoidType())],
"scores_gen": [NeuralType(('B', 'C', 'T_out'), VoidType())],
"fmaps_real": [[NeuralType(('B', 'D', 'T_layer', 'C'), VoidType())]],
"fmaps_gen": [[NeuralType(('B', 'D', 'T_layer', 'C'), VoidType())]],
}
@typecheck()
def forward(self, audio_real, audio_gen):
y_d_r, fmap_r = self._forward(audio_real)
y_d_g, fmap_g = self._forward(audio_gen)
return [y_d_r.unsqueeze(1)], [y_d_g.unsqueeze(1)], [fmap_r], [fmap_g]
class SLMEncoder(NeuralModule):
"""Encoder wrapping a speech language model (SLM) which produces semantic embeddings for use in semantic distillation.
Args:
slm_model_name: Name of Hugging Face model.
slm_sr: Sample rate SLM model requires for input.
input_sr: Sampling rate of audio that will be input to this encoder.
hidden_layer: Index of hidden layer to extract embeddings from.
Defaults to 16, which for research suggests is effective for w2v-bert and TTS.
padding: Number of audio samples to pad before encoding to ensure output has a frame rate compatible with the audio codec.
scaling_factor: Constant factor to divide output embedding by. Defaults to 5 to produce embeddings with values in [-1, 1].
"""
def __init__(
self,
slm_model_name="facebook/w2v-bert-2.0",
slm_sr=16000,
input_sr=22050,
hidden_layer=16,
padding=80,
scaling_factor=5.0,
):
super().__init__()
self.slm_sr = slm_sr
if input_sr == self.slm_sr:
self.resample = None
else:
self.resample = Resample(orig_freq=input_sr, new_freq=self.slm_sr)
self.feature_extractor = AutoFeatureExtractor.from_pretrained(slm_model_name)
self.semantic_model = Wav2Vec2BertModel.from_pretrained(slm_model_name, output_hidden_states=True)
self.semantic_model.eval()
self.hidden_layer = hidden_layer
self.padding = padding
self.scaling_factor = scaling_factor
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T'), AudioSignal()),
}
@property
def output_types(self):
return {
"slm_embeddings": [NeuralType(('B', 'D', 'T'), VoidType())],
}
@typecheck()
def forward(self, audio):
if self.resample is not None:
audio = self.resample(audio)
audio = torch.nn.functional.pad(audio, (0, self.padding))
feats = self.feature_extractor(audio.cpu(), sampling_rate=self.slm_sr, return_tensors="pt").data[
'input_features'
]
feats = feats.to(audio.device)
with torch.no_grad():
out = self.semantic_model(feats)
slm_emb = out.hidden_states[self.hidden_layer] / self.scaling_factor
slm_emb = rearrange(slm_emb, 'B T D -> B D T')
return slm_emb
class SLMPredictor(NeuralModule):
"""Module for predicting SLM embeddings for semantic distillation. This decoder uses transposed convolutions to upsample from
the codecs frame rate to the frame rate of the SLM model.
Args:
in_channels: Input dimension of quantized codec encoding.
hidden_dim: Hidden dimension that input will be projected to.
out_channels: Dimension of decoder embedding
up_sample_rate: Rate to up sample by to match SLM frame rate.
kernel_size: Kernel size of convolutions.
padding_mode: Padding used with convolutions.
activation: Activation to use in between convolutions
"""
def __init__(
self,
in_channels: int,
hidden_dim: int,
out_channels: int,
up_sample_rate: int = 1,
kernel_size: int = 3,
padding_mode: str = "replicate",
activation: str = "lrelu",
):
super().__init__()
padding = get_padding(kernel_size=kernel_size)
self.activation = CodecActivation(activation=activation)
self.input_layer = nn.Conv1d(
in_channels=in_channels,
out_channels=hidden_dim,
kernel_size=kernel_size,
padding=padding,
padding_mode=padding_mode,
)
self.output_layer = nn.Conv1d(
in_channels=hidden_dim,
out_channels=out_channels,
kernel_size=kernel_size,
padding=padding,
padding_mode=padding_mode,
)
if up_sample_rate > 1:
up_kernel_size = 2 * up_sample_rate
up_padding, output_padding = get_up_sample_padding(up_kernel_size, up_sample_rate)
self.upsample_layer = nn.Sequential(
nn.ConvTranspose1d(
in_channels=hidden_dim,
out_channels=hidden_dim,
kernel_size=up_kernel_size,
stride=up_sample_rate,
padding=up_padding,
output_padding=output_padding,
),
self.activation,
)
else:
self.upsample_layer = nn.Identity()
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'D', 'T'), VoidType()),
}
@property
def output_types(self):
return {
"output": NeuralType(('B', 'C', 'T'), VoidType()),
}
@typecheck()
def forward(self, inputs):
out = self.input_layer(inputs)
out = self.activation(out)
out = self.upsample_layer(out)
out = self.activation(out)
out = self.output_layer(out)
return out
# Torch version of transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
def zero_mean_unit_var_norm(input_values):
"""
Normalized to have zero mean and unit variance
"""
normed_input_values = (input_values - input_values.mean(dim=1).unsqueeze(-1)) / torch.sqrt(
input_values.var(dim=1).unsqueeze(-1) + 1e-7
)
return normed_input_values
##############
# Speaker encoder #
##############
def load_fsspec(path: str, map_location: str = None, **kwargs):
"""Like torch.load but can load from other locations (e.g. s3:// , gs://).
Args:
path: Any path or url supported by fsspec.
map_location: torch.device or str.
cache: If True, cache a remote file locally for subsequent calls. It is cached under `get_user_data_dir()/tts_cache`. Defaults to True.
**kwargs: Keyword arguments forwarded to torch.load.
Returns:
Object stored in path.
"""
is_local = os.path.isdir(path) or os.path.isfile(path)
if is_local:
return torch.load(path, map_location=map_location, **kwargs)
else:
if HAVE_FSSPEC:
with fsspec.open(path, "rb") as f:
return torch.load(f, map_location=map_location, **kwargs)
else:
logging.error('Could not import fsspec. Loading a checkpoint link is not supported!')
raise ModuleNotFoundError("fsspec is not installed but is necessary to download remote checkpoints !!")
class PreEmphasis(NeuralModule):
def __init__(self, coefficient=0.97):
super().__init__()
self.coefficient = coefficient
self.register_buffer("filter", torch.FloatTensor([-self.coefficient, 1.0]).unsqueeze(0).unsqueeze(0))
def forward(self, x):
assert len(x.size()) == 2
x = torch.nn.functional.pad(x.unsqueeze(1), (1, 0), "reflect")
return torch.nn.functional.conv1d(x, self.filter).squeeze(1)
class SELayer(NeuralModule):
def __init__(self, channel, reduction=8):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel),
nn.Sigmoid(),
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y
class SEBasicBlock(NeuralModule):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None, reduction=8):
super(SEBasicBlock, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.se = SELayer(planes, reduction)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.relu(out)
out = self.bn1(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.se(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNetSpeakerEncoder(NeuralModule):
"""Implementation of the model H/ASP without batch normalization in speaker embedding. This model was proposed in: https://arxiv.org/abs/2009.14153
Adapted from: https://github.com/clovaai/voxceleb_trainer
"""
def __init__(
self,
input_dim=64,
proj_dim=512,
layers=[3, 4, 6, 3],
num_filters=[32, 64, 128, 256],
encoder_type="ASP",
log_input=True,
use_torch_spec=True,
audio_config={
"fft_size": 512,
"win_length": 400,
"hop_length": 160,
"frame_shift_ms": None,
"frame_length_ms": None,
"stft_pad_mode": "reflect",
"sample_rate": 16000,
"resample": False,
"preemphasis": 0.97,
"ref_level_db": 20,
"do_sound_norm": False,
"do_trim_silence": False,
"trim_db": 60,
"power": 1.5,
"griffin_lim_iters": 60,
"num_mels": 64,
"mel_fmin": 0.0,
"mel_fmax": 8000.0,
"spec_gain": 20,
"signal_norm": False,
"min_level_db": -100,
"symmetric_norm": False,
"max_norm": 4.0,
"clip_norm": False,
"stats_path": None,
"do_rms_norm": True,
"db_level": -27.0,
},
):
super(ResNetSpeakerEncoder, self).__init__()
self.encoder_type = encoder_type
self.input_dim = input_dim
self.log_input = log_input
self.use_torch_spec = use_torch_spec
self.audio_config = audio_config
self.proj_dim = proj_dim
self.conv1 = nn.Conv2d(1, num_filters[0], kernel_size=3, stride=1, padding=1)
self.relu = nn.ReLU(inplace=True)
self.bn1 = nn.BatchNorm2d(num_filters[0])
self.inplanes = num_filters[0]
self.layer1 = self.create_layer(SEBasicBlock, num_filters[0], layers[0])
self.layer2 = self.create_layer(SEBasicBlock, num_filters[1], layers[1], stride=(2, 2))
self.layer3 = self.create_layer(SEBasicBlock, num_filters[2], layers[2], stride=(2, 2))
self.layer4 = self.create_layer(SEBasicBlock, num_filters[3], layers[3], stride=(2, 2))
self.instancenorm = nn.InstanceNorm1d(input_dim)
self.torch_spec = self.get_torch_mel_spectrogram_class(audio_config) if self.use_torch_spec else None
outmap_size = int(self.input_dim / 8)
self.attention = nn.Sequential(
nn.Conv1d(num_filters[3] * outmap_size, 128, kernel_size=1),
nn.ReLU(),
nn.BatchNorm1d(128),
nn.Conv1d(128, num_filters[3] * outmap_size, kernel_size=1),
nn.Softmax(dim=2),
)
if self.encoder_type == "SAP":
out_dim = num_filters[3] * outmap_size
elif self.encoder_type == "ASP":
out_dim = num_filters[3] * outmap_size * 2
else:
raise ValueError("Undefined encoder")
self.fc = nn.Linear(out_dim, proj_dim)
self._init_layers()
def _init_layers(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def create_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def new_parameter(self, *size):
out = nn.Parameter(torch.FloatTensor(*size))
nn.init.xavier_normal_(out)
return out
def forward(self, x, l2_norm=False):
"""Forward pass of the model.
Args:
x (Tensor): Raw waveform signal or spectrogram frames. If input is a waveform, `torch_spec` must be `True`
to compute the spectrogram on-the-fly.
l2_norm (bool): Whether to L2-normalize the outputs.
Shapes:
- x: :math:`(N, 1, T_{in})` or :math:`(N, D_{spec}, T_{in})`
"""
with default_precision(torch.float32):
x.squeeze_(1)
# if you torch spec compute it otherwise use the mel spec computed by the AP
if self.use_torch_spec:
x = self.torch_spec(x)
if self.log_input:
x = (x + 1e-6).log()
x = self.instancenorm(x).unsqueeze(1)
x = self.conv1(x)
x = self.relu(x)
x = self.bn1(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = x.reshape(x.size()[0], -1, x.size()[-1])
w = self.attention(x)
if self.encoder_type == "SAP":
x = torch.sum(x * w, dim=2)
elif self.encoder_type == "ASP":
mu = torch.sum(x * w, dim=2)
sg = torch.sqrt((torch.sum((x**2) * w, dim=2) - mu**2).clamp(min=1e-5))
x = torch.cat((mu, sg), 1)
x = x.view(x.size()[0], -1)
x = self.fc(x)
if l2_norm:
x = torch.nn.functional.normalize(x, p=2, dim=1)
return x
def get_torch_mel_spectrogram_class(self, audio_config):
return torch.nn.Sequential(
PreEmphasis(audio_config["preemphasis"]),
MelSpectrogram(
sample_rate=audio_config["sample_rate"],
n_fft=audio_config["fft_size"],
win_length=audio_config["win_length"],
hop_length=audio_config["hop_length"],
window_fn=torch.hamming_window,
n_mels=audio_config["num_mels"],
),
)
def load_checkpoint(self, checkpoint_path: str, strict=True):
state = load_fsspec(checkpoint_path, map_location=torch.device("cpu"))
self.load_state_dict(state["model"], strict=strict)
class CodecActivation(nn.Module):
"""
Choose between activation based on the input parameter.
Args:
activation: Name of activation to use. Valid options are "elu" (default), "lrelu", and "snake".
channels: Input dimension.
"""
def __init__(self, activation: str = "elu", channels: int = 1):
super().__init__()
activation = activation.lower()
if activation == "elu":
self.activation = nn.ELU()
elif activation == "lrelu":
self.activation = torch.nn.LeakyReLU()
elif activation == "snake":
self.activation = Snake(channels)
elif activation == "half_snake":
self.activation = HalfSnake(channels)
else:
raise ValueError(f"Unknown activation {activation}")
def forward(self, x):
return self.activation(x)
class CausalConvTranspose1dNorm(NeuralModule):
"""ConvTranspose1d causal padding and normalization."""
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
stride: int = 1,
groups: int = None,
activation: Optional[str] = None,
trim_right_ratio: int = 1,
bias=True,
):
super().__init__()
self.trim_right_ratio = trim_right_ratio
# if groups are None, create a group for each out channel as done in Mini Codec
groups = out_channels if groups is None else groups
self.conv = nn.ConvTranspose1d(in_channels, out_channels, kernel_size, stride, groups=groups, bias=bias)
if activation is not None:
self.activation = CodecActivation(activation=activation, channels=out_channels)
else:
self.activation = nn.Identity()
kernel_size = self.conv.kernel_size[0]
stride = self.conv.stride[0]
padding_total = kernel_size - stride
# Trim the padding on the right according to the specified ratio
# if trim_right_ratio = 1.0, trim everything from right
self.padding_right = math.ceil(padding_total * self.trim_right_ratio)
self.padding_left = padding_total - self.padding_right
# add weight norm
self.conv = nn.utils.parametrizations.weight_norm(self.conv)
def apply_weight_norm(self):
weight_norm = nn.utils.parametrizations.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm
weight_norm(self.conv)
def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv)
def forward(self, inputs, input_len):
hidden_states = self.conv(inputs)
# unpad
end = hidden_states.shape[-1] - self.padding_right
hidden_states = hidden_states[..., self.padding_left : end]
hidden_states = self.activation(hidden_states)
# mask
hidden_states = mask_sequence_tensor(hidden_states, input_len)
return hidden_states
class CausalConv1dNorm(NeuralModule):
"""Conv1d with causal padding and normalization."""
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
stride: int = 1,
dilation: int = 1,
groups: int = 1,
activation: Optional[str] = None,
pad_mode: str = "zeros",
extra_pad_mode: str = "constant",
bias: bool = True,
):
super().__init__()
self.extra_pad_mode = extra_pad_mode
# warn user on unusual setup between dilation and stride
if stride > 1 and dilation > 1:
print(
"CausalConv1dNorm has been initialized with stride > 1 and dilation > 1"
f" (kernel_size={kernel_size} stride={stride}, dilation={dilation})."
)
self.conv = nn.Conv1d(
in_channels,
out_channels,
kernel_size,
stride,
dilation=dilation,
groups=groups,
bias=bias,
padding_mode=pad_mode,
)
if activation is not None:
self.activation = CodecActivation(activation=activation, channels=out_channels)
else:
self.activation = nn.Identity()
kernel_size = self.conv.kernel_size[0]
stride = torch.tensor(self.conv.stride[0], dtype=torch.int64)
dilation = self.conv.dilation[0]
# Effective kernel size with dilations.
kernel_size = torch.tensor((kernel_size - 1) * dilation + 1, dtype=torch.int64)
self.register_buffer("stride", stride, persistent=False)
self.register_buffer("kernel_size", kernel_size, persistent=False)
self.register_buffer("padding_total", torch.tensor(kernel_size - stride, dtype=torch.int64), persistent=False)
# add weight norm
self.conv = nn.utils.parametrizations.weight_norm(self.conv)
def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv)
# Copied from transformers.models.encodec.modeling_encodec.EncodecConv1d._get_extra_padding_for_conv1d
def _get_extra_padding_for_conv1d(
self,
hidden_states: torch.Tensor,
) -> torch.Tensor:
"""See `pad_for_conv1d`."""
with default_precision(torch.float32):
length = hidden_states.shape[-1]
n_frames = (length - self.kernel_size + self.padding_total) / self.stride + 1
n_frames = torch.ceil(n_frames).to(torch.int64) - 1
ideal_length = (n_frames * self.stride).long() + self.kernel_size - self.padding_total
return (ideal_length - length).long()
@staticmethod
# Copied from transformers.models.encodec.modeling_encodec.EncodecConv1d._pad1d
def _pad1d(hidden_states: torch.Tensor, paddings: Tuple[int, int], mode: str = "zero", value: float = 0.0):
"""Tiny wrapper around torch.nn.functional.pad, just to allow for reflect padding on small input.
If this is the case, we insert extra 0 padding to the right before the reflection happens.
"""
length = hidden_states.shape[-1]
padding_left, padding_right = paddings
if not mode == "reflect":
return nn.functional.pad(hidden_states, paddings, mode, value)
max_pad = max(padding_left, padding_right)
extra_pad = 0
if length <= max_pad:
extra_pad = max_pad - length + 1
hidden_states = nn.functional.pad(hidden_states, (0, extra_pad))
padded = nn.functional.pad(hidden_states, paddings, mode, value)
end = padded.shape[-1] - extra_pad
return padded[..., :end]
def forward(self, inputs, input_len):
extra_padding = self._get_extra_padding_for_conv1d(inputs)
# Left padding for causal
hidden_states = self._pad1d(inputs, (self.padding_total, extra_padding), mode=self.extra_pad_mode)
hidden_states = self.conv(hidden_states)
hidden_states = self.activation(hidden_states)
# mask output
hidden_states = mask_sequence_tensor(hidden_states, input_len)
return hidden_states
class Conv1dNorm(NeuralModule):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
stride: int = 1,
dilation: int = 1,
padding: Optional[int] = None,
pad_mode: str = "reflect",
activation: Optional[str] = None,
):
super().__init__()
if not padding:
padding = get_padding(kernel_size=kernel_size, dilation=dilation)
conv = nn.Conv1d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
padding_mode=pad_mode,
)
self.conv = nn.utils.parametrizations.weight_norm(conv)
if activation is not None:
self.activation = CodecActivation(activation=activation, channels=out_channels)
else:
self.activation = torch.nn.Identity()
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'C', 'T'), VoidType()),
"input_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"out": NeuralType(('B', 'C', 'T'), VoidType()),
}
def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv)
@typecheck()
def forward(self, inputs, input_len):
out = self.conv(inputs)
out = self.activation(out)
out = mask_sequence_tensor(out, input_len)
return out
class ConvTranspose1dNorm(NeuralModule):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
stride: int = 1,
groups: int = 1,
activation: Optional[str] = None,
):
super().__init__()
padding, output_padding = get_up_sample_padding(kernel_size, stride)
conv = nn.ConvTranspose1d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
output_padding=output_padding,
padding_mode="zeros",
groups=groups,
)
self.conv = nn.utils.parametrizations.weight_norm(conv)
if activation is not None:
self.activation = CodecActivation(activation=activation, channels=out_channels)
else:
self.activation = nn.Identity()
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'C', 'T'), VoidType()),
"input_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"out": NeuralType(('B', 'C', 'T'), VoidType()),
}
def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv)
@typecheck()
def forward(self, inputs, input_len):
out = self.conv(inputs)
out = self.activation(out)
out = mask_sequence_tensor(out, input_len)
return out
class Conv2dNorm(NeuralModule):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: Tuple[int, int],
stride: Tuple[int, int] = (1, 1),
dilation: Tuple[int, int] = (1, 1),
):
super().__init__()
assert len(kernel_size) == len(dilation)
padding = get_padding_2d(kernel_size, dilation)
conv = nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
dilation=dilation,
padding=padding,
padding_mode="reflect",
)
self.conv = nn.utils.parametrizations.weight_norm(conv)
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'C', 'H', 'T'), VoidType()),
}
@property
def output_types(self):
return {
"out": NeuralType(('B', 'C', 'H', 'T'), VoidType()),
}
def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv)
@typecheck()
def forward(self, inputs):
return self.conv(inputs)
class PeriodDiscriminator(NeuralModule):
"""
Period discriminator introduced in HiFi-GAN https://arxiv.org/abs/2010.05646 which attempts to
discriminate phase information by looking at equally spaced audio samples.
Args:
period: Spacing between audio sample inputs.
lrelu_slope: Slope to use for activation. Leaky relu with slope of 0.1 or 0.2 is recommended for the
stability of the feature matching loss.
"""
def __init__(self, period, lrelu_slope=0.1):
super().__init__()
self.period = period
self.activation = nn.LeakyReLU(lrelu_slope)
self.conv_layers = nn.ModuleList(
[
Conv2dNorm(1, 32, kernel_size=(5, 1), stride=(3, 1)),
Conv2dNorm(32, 128, kernel_size=(5, 1), stride=(3, 1)),
Conv2dNorm(128, 512, kernel_size=(5, 1), stride=(3, 1)),
Conv2dNorm(512, 1024, kernel_size=(5, 1), stride=(3, 1)),
Conv2dNorm(1024, 1024, kernel_size=(5, 1), stride=(1, 1)),
]
)
self.conv_post = Conv2dNorm(1024, 1, kernel_size=(3, 1))
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
}
@property
def output_types(self):
return {
"score": NeuralType(('B', 'C', 'T_out'), VoidType()),
"fmap": [NeuralType(('B', 'D', 'T_layer', 'C'), VoidType())],
}
@typecheck()
def forward(self, audio):
batch_size, time = audio.shape
out = rearrange(audio, 'B T -> B 1 T')
# Pad audio so that it is divisible by the period
if time % self.period != 0:
n_pad = self.period - (time % self.period)
out = F.pad(out, (0, n_pad), "reflect")
time = time + n_pad
# [batch, 1, (time / period), period]
out = out.view(batch_size, 1, time // self.period, self.period)
fmap = []
for conv in self.conv_layers:
# [batch, filters, (time / period / stride), period]
out = conv(inputs=out)
out = self.activation(out)
fmap.append(out)
# [batch, 1, (time / period / strides), period]
score = self.conv_post(inputs=out)
fmap.append(score)
score = rearrange(score, "B 1 T C -> B C T")
return score, fmap
class MultiPeriodDiscriminator(NeuralModule):
"""
Wrapper class to aggregate results of multiple period discriminators.
The periods are expected to be increasing prime numbers in order to maximize coverage and minimize overlap
"""
def __init__(self, periods: Iterable[int] = (2, 3, 5, 7, 11), lrelu_slope=0.1):
super().__init__()
self.discriminators = nn.ModuleList(
[PeriodDiscriminator(period=period, lrelu_slope=lrelu_slope) for period in periods]
)
@property
def input_types(self):
return {
"audio_real": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_gen": NeuralType(('B', 'T_audio'), AudioSignal()),
}
@property
def output_types(self):
return {
"scores_real": [NeuralType(('B', 'C', 'T_out'), VoidType())],
"scores_gen": [NeuralType(('B', 'C', 'T_out'), VoidType())],
"fmaps_real": [[NeuralType(('B', 'D', 'T_layer', 'C'), VoidType())]],
"fmaps_gen": [[NeuralType(('B', 'D', 'T_layer', 'C'), VoidType())]],
}
@typecheck()
def forward(self, audio_real, audio_gen):
scores_real = []
scores_gen = []
fmaps_real = []
fmaps_gen = []
for discriminator in self.discriminators:
score_real, fmap_real = discriminator(audio=audio_real)
score_gen, fmap_gen = discriminator(audio=audio_gen)
scores_real.append(score_real)
fmaps_real.append(fmap_real)
scores_gen.append(score_gen)
fmaps_gen.append(fmap_gen)
return scores_real, scores_gen, fmaps_real, fmaps_gen
class DiscriminatorSTFT(NeuralModule):
"""
Discriminator network from EnCodec for Complex STFT input, but without dilations.
Args:
filters: number of filters to use in Conv2d layers
lrelu_slope: Slope to use for activations. Leaky relu with slope of 0.1 or 0.2 is recommended for the
stability of the feature matching loss
"""
def __init__(self, filters: int = 32, lrelu_slope: float = 0.1):
super().__init__()
self.activation = nn.LeakyReLU(lrelu_slope)
self.conv_layers = nn.ModuleList(
[
Conv2dNorm(2, filters, kernel_size=(3, 9)),
Conv2dNorm(filters, filters, kernel_size=(3, 9), stride=(1, 2)),
Conv2dNorm(filters, filters, kernel_size=(3, 9), stride=(1, 2)),
Conv2dNorm(filters, filters, kernel_size=(3, 9), stride=(1, 2)),
Conv2dNorm(filters, filters, kernel_size=(3, 3)),
]
)
self.conv_post = Conv2dNorm(filters, 1, kernel_size=(3, 3))
@property
def input_types(self):
return {
"spec": NeuralType(('B', 'C', 'T_spec', 'D'), VoidType()),
}
@property
def output_types(self):
return {
"scores": NeuralType(('B', 'C', 'T_spec'), VoidType()),
"fmap": [NeuralType(('B', 'D', 'T_spec', 'C'), VoidType())],
}
@typecheck()
def forward(self, spec):
fmap = []
# [batch, 2, T_spec, fft]
out = spec
for conv in self.conv_layers:
# [batch, filters, T_spec, fft // strides]
out = conv(inputs=out)
out = self.activation(out)
fmap.append(out)
# [batch, 1, T_spec, fft // 8]
scores = self.conv_post(inputs=out)
fmap.append(scores)
scores = rearrange(scores, "B 1 T C -> B C T")
return scores, fmap
class MultiBandDiscriminatorSTFT(NeuralModule):
"""
Multi-band STFT discriminator proposed in DAC (https://arxiv.org/abs/2306.06546).
Computes the complex STFT for a given resolution and splits it into sub-bands,
which are given to separate discriminator networks.
Args:
resolution: STFT resolution, provided as a tuple of 3 integers ordered (num_fft, hop_length, window_length)
stft_bands: List of tuples, with each tuple having 2 float values (band_start, band_end).
The floats are in the range [0, 1] representing the fraction of all stft bands.
For example for n_fft=1024, the stft output has 513 dimensions.
For band input [(0, 0.25), (0.25, 1.0)] it would use stft dimensions [0 through 127] and [128 through 512].
"""
def __init__(self, resolution: Tuple[int], stft_bands: Iterable[Tuple[int]]):
super().__init__()
self.n_fft, self.hop_length, self.win_length = resolution
self.register_buffer("window", torch.hann_window(self.win_length, periodic=False))
self.discriminators = nn.ModuleList([DiscriminatorSTFT() for _ in stft_bands])
n_stft = self.n_fft // 2 + 1
self.stft_bands = [(int(band[0] * n_stft), int(band[1] * n_stft)) for band in stft_bands]
def compute_stft(self, audio):
# [B, fft, T_spec]
fft = torch.stft(
audio,
n_fft=self.n_fft,
hop_length=self.hop_length,
win_length=self.win_length,
window=self.window,
normalized=True,
center=True,
return_complex=True,
)
fft = rearrange(fft, "B fft T -> B T fft")
# [batch, 2, T_spec, fft]
out = torch.stack([fft.real, fft.imag], dim=1)
return out
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
}
@property
def output_types(self):
return {
"scores_list": [NeuralType(('B', 'C', 'T_spec'), VoidType())],
"fmaps_list": [[NeuralType(('B', 'D', 'T_spec', 'C'), VoidType())]],
}
@typecheck()
def forward(self, audio):
scores_list = []
fmap_list = []
# run spec compute on fp32 and convert out to the model training type
spec = self.compute_stft(audio.float()).to(audio.dtype)
for band, disc in zip(self.stft_bands, self.discriminators):
spec_band = spec[:, :, :, band[0] : band[1]]
score, fmap = disc(spec=spec_band)
scores_list.append(score)
fmap_list.append(fmap)
return scores_list, fmap_list
class MultiResolutionDiscriminatorSTFT(NeuralModule):
"""
Multi-resolution discriminator which creates a multi-band discriminator for each input resolution.
Args:
resolutions: List of STFT resolutions, each resolution provided as a tuple of 3 integers ordered
(num_fft, hop_length, window_length)
stft_bands: List of tuples, with each tuple having 2 float values (band_start, band_end).
The floats are in the range [0, 1] representing the fraction of all stft bands.
For example for n_fft=1024, the stft output has 513 dimensions.
For band input [(0, 0.25), (0.25, 1.0)] it would use stft dimensions [0 through 127] and [128 through 512].
"""
def __init__(self, resolutions: Iterable[Tuple[int]], stft_bands: Iterable[Tuple[int]]):
super().__init__()
self.discriminators = nn.ModuleList(
[MultiBandDiscriminatorSTFT(resolution=resolution, stft_bands=stft_bands) for resolution in resolutions]
)
@property
def input_types(self):
return {
"audio_real": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_gen": NeuralType(('B', 'T_audio'), AudioSignal()),
}
@property
def output_types(self):
return {
"scores_real": [NeuralType(('B', 'C', 'T_spec'), VoidType())],
"scores_gen": [NeuralType(('B', 'C', 'T_spec'), VoidType())],
"fmaps_real": [[NeuralType(('B', 'D', 'T_spec', 'C'), VoidType())]],
"fmaps_gen": [[NeuralType(('B', 'D', 'T_spec', 'C'), VoidType())]],
}
@typecheck()
def forward(self, audio_real, audio_gen):
scores_real = []
scores_gen = []
fmaps_real = []
fmaps_gen = []
for disc in self.discriminators:
score_real_i, fmap_real_i = disc(audio=audio_real)
scores_real = scores_real + score_real_i
fmaps_real = fmaps_real + fmap_real_i
score_gen_i, fmap_gen_i = disc(audio=audio_gen)
scores_gen = scores_gen + score_gen_i
fmaps_gen = fmaps_gen + fmap_gen_i
return scores_real, scores_gen, fmaps_real, fmaps_gen
class Discriminator(NeuralModule):
"""
Wrapper class which takes a list of discriminators and aggregates the results across them.
"""
def __init__(self, discriminators: Iterable[NeuralModule]):
super().__init__()
self.discriminators = nn.ModuleList(discriminators)
@property
def input_types(self):
return {
"audio_real": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_gen": NeuralType(('B', 'T_audio'), AudioSignal()),
}
@property
def output_types(self):
return {
"scores_real": [NeuralType(('B', 'C', 'T_out'), VoidType())],
"scores_gen": [NeuralType(('B', 'C', 'T_out'), VoidType())],
"fmaps_real": [[NeuralType(('B', 'D', 'T_layer', 'C'), VoidType())]],
"fmaps_gen": [[NeuralType(('B', 'D', 'T_layer', 'C'), VoidType())]],
}
@typecheck()
def forward(self, audio_real, audio_gen):
scores_real = []
scores_gen = []
fmaps_real = []
fmaps_gen = []
for discriminator in self.discriminators:
score_real, score_gen, fmap_real, fmap_gen = discriminator(audio_real=audio_real, audio_gen=audio_gen)
scores_real += score_real
fmaps_real += fmap_real
scores_gen += score_gen
fmaps_gen += fmap_gen
return scores_real, scores_gen, fmaps_real, fmaps_gen
class VectorQuantizerBase(NeuralModule, ABC):
@property
@abstractmethod
def num_codebooks(self) -> int:
pass
@property
@abstractmethod
def codebook_size(self) -> int:
pass
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
"input_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"dequantized": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
"indices": NeuralType(('D', 'B', 'T'), TokenIndex()),
}
@typecheck()
@abstractmethod
def forward(self, inputs: torch.Tensor, input_len: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
pass
@typecheck(
input_types={
"inputs": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
"input_len": NeuralType(tuple('B'), LengthsType()),
},
output_types={"indices": NeuralType(('D', 'B', 'T'), TokenIndex())},
)
@abstractmethod
def encode(self, inputs: torch.Tensor, input_len: torch.Tensor) -> torch.Tensor:
pass
@typecheck(
input_types={
"indices": NeuralType(('D', 'B', 'T'), TokenIndex()),
"input_len": NeuralType(tuple('B'), LengthsType()),
},
output_types={
"dequantized": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
},
)
@abstractmethod
def decode(self, indices: torch.Tensor, input_len: torch.Tensor) -> torch.Tensor:
pass
class FiniteScalarQuantizer(VectorQuantizerBase):
"""This quantizer is based on the Finite Scalar Quantization (FSQ) method.
It quantizes each element of the input vector independently into a number of levels.
Args:
num_levels: number of levels for each dimension/element of the input vector
eps: small regularization constant for scaling
References:
Mentzer et al., Finite Scalar Quantization: VQ-VAE Made Simple (https://arxiv.org/abs/2309.15505v1)
"""
def __init__(self, num_levels: List[int], eps: float = 1e-3):
super().__init__()
# index base per dimension of the input vector
# this is used to convert between per-dimension indices and a codebook token index
dim_base_index = torch.cumprod(torch.tensor([1] + num_levels[:-1]), dim=0, dtype=torch.int32)
dim_base_index = rearrange(dim_base_index, 'D -> 1 D 1')
self.register_buffer('dim_base_index', dim_base_index)
# Register the number of levels for each dimension
num_levels = torch.tensor(num_levels, dtype=torch.int32)
num_levels = rearrange(num_levels, 'D -> 1 D 1')
self.register_buffer('num_levels', num_levels)
# Regularization
self.eps = eps
logging.debug('Initializing %s with', self.__class__.__name__)
logging.debug('\tdim: %s', self.dim)
logging.debug('\tnum_levels: %s', self.num_levels)
logging.debug('\tcodebook_size: %s', self.codebook_size)
logging.debug('\teps: %s', self.eps)
@property
def num_codebooks(self):
"""Returns the number of codebooks."""
return 1
@property
def codebook_size(self):
"""Returns the size of the corresponding codebook."""
return self.num_levels.prod().item()
@property
def dim(self):
"""Returns the dimension of the input vector."""
return self.num_levels.numel()
@property
def codebook_dim(self):
"""Returns the dimension of the input vector.
Keeping for compatiblitiy with the original RVQ implementation.
"""
return self.dim
@property
def codes(self):
"""Returns the codebooks entries.
Note that the codebook entries are implicitly defined by the number of levels.
"""
indices = torch.arange(self.codebook_size, device=self.dim_base_index.device)
# [D, B, T]
indices = rearrange(indices, 'B -> 1 B 1')
# [B, D, T]
codes = self.decode(indices=indices, input_len=None)
# Remove the time dimension
codes = codes.squeeze(-1)
return codes
@property
def codebook(self):
"""Returns the codebooks entries.
See self.codes for more details.
"""
return self.codes
@staticmethod
def round(inputs: torch.Tensor, input_len: torch.Tensor) -> torch.Tensor:
"""Round the input tensor to nearest integer
and use a straight-through estimator for the gradient.
"""
inputs_rounded = torch.round(inputs)
return inputs + (inputs_rounded - inputs).detach()
def compress(self, inputs: torch.Tensor, input_len: torch.Tensor) -> torch.Tensor:
"""Apply compression to the input, to limit to values."""
output_scale = (self.num_levels - 1) / 2
# scale down a bit to avoid rounding issues
output_scale = output_scale * (1 - self.eps)
# offset for even number of levels
output_offset = torch.where(self.num_levels % 2 == 0, 0.5, 0)
# shift for even number of levels
input_shift = (output_offset / output_scale).tan()
# compressed output
output = output_scale * (inputs + input_shift).tanh() - output_offset
return output
@typecheck(
input_types={
"inputs": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
"input_len": NeuralType(tuple('B'), LengthsType()),
},
output_types={"codes": NeuralType(('B', 'D', 'T'), TokenIndex())},
)
def inputs_to_codes(self, inputs: torch.Tensor, input_len: torch.Tensor) -> torch.Tensor:
# apply compression
compressed = self.compress(inputs=inputs, input_len=input_len)
# apply rounding to nearest integer
codes = self.round(inputs=compressed, input_len=input_len)
# normalize to [-1, 1]
scale = self.num_levels // 2
codes = codes / scale
return codes
def codes_to_nonnegative(self, codes: torch.Tensor) -> torch.Tensor:
"""Convert values centered arouund zero to nonnegative values."""
scale = offset = self.num_levels // 2
return scale * codes + offset
def nonnegative_to_codes(self, codes_nonnegative: torch.Tensor) -> torch.Tensor:
"""Convert nonnegative values to values centered arouund zero."""
scale = offset = self.num_levels // 2
return (codes_nonnegative - offset) / scale
def codes_to_indices(self, codes: torch.Tensor) -> torch.Tensor:
"""Converts a code vector to a single index."""
if codes.size(1) != self.dim:
raise RuntimeError(
f'Input code dimension {codes.size(1)} not matching the expected dimension {self.dim}, input codes shape {codes.shape}'
)
# convert code vectors to nonnegative values
indices = self.codes_to_nonnegative(codes)
# convert one nonnegative index per dimension to a single index per code vector
indices = torch.sum(indices * self.dim_base_index, dim=1)
return indices.to(torch.int32)
# Implementation of VectorQuantiserBase API
@typecheck()
def forward(
self, inputs: torch.Tensor, input_len: Optional[torch.Tensor] = None
) -> Tuple[torch.Tensor, torch.Tensor]:
if inputs.size(1) != self.dim:
raise RuntimeError(
f'Input dimension {inputs.size(1)} not matching the expected dimension {self.dim}, inputs shape {inputs.shape}'
)
dequantized = self.inputs_to_codes(inputs=inputs, input_len=input_len)
indices = self.codes_to_indices(codes=dequantized)
if input_len is not None:
# apply masking
dequantized = mask_sequence_tensor(dequantized, input_len)
indices = mask_sequence_tensor(indices, input_len)
# only 1 codebook, but return in [D, B, T] format to match RVQ API
indices = indices.unsqueeze(0)
return dequantized, indices
@typecheck(
input_types={
"inputs": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
"input_len": NeuralType(tuple('B'), LengthsType(), optional=True),
},
output_types={"indices": NeuralType(('D', 'B', 'T'), TokenIndex())},
)
def encode(self, inputs: torch.Tensor, input_len: Optional[torch.Tensor] = None) -> torch.Tensor:
"""Convert a continuous code vector to a single index."""
_, indices = self(inputs=inputs, input_len=input_len)
return indices
@typecheck(
input_types={
"indices": NeuralType(('D', 'B', 'T'), TokenIndex()),
"input_len": NeuralType(tuple('B'), LengthsType(), optional=True),
},
output_types={
"dequantized": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
},
)
def decode(self, indices: torch.Tensor, input_len: Optional[torch.Tensor] = None) -> torch.Tensor:
"""Convert a single index to a continuous code vector."""
if indices.size(0) > 1:
# codebook dimension used for compatibility with RVQ
raise ValueError(
f'Expected a single codebook, got {indices.size(0)} codebooks for indices with shape {indices.shape}.'
)
indices = rearrange(indices, 'D B T -> B D T')
# convert a single index to nonnegative index per-dimension
codes_nonnegative = (indices // self.dim_base_index) % self.num_levels
# convert nonnegative codes to codes (centered around zero)
dequantized = self.nonnegative_to_codes(codes_nonnegative)
if input_len is not None:
# apply masking
dequantized = mask_sequence_tensor(dequantized, input_len)
return dequantized
class GroupFiniteScalarQuantizer(VectorQuantizerBase):
"""Split the input vector into groups and apply FSQ on each group separately.
This class is for convenience. Since FSQ is applied on each group separately,
groups can be defined arbitrarily by splitting the input vector. However, this
class makes it easy to construct several groups with the same quantization num_levels.
Args:
num_groups: number of groups to split the input into, each group will be quantized separately using num_codebooks//num_groups codebooks
codebook_dim: embedding dimension, will be split into num_groups
**kwargs: parameters of FiniteScalarQuantizer
References:
Yang et al, HiFi-Codec: Group-residual Vector quantization for High Fidelity Audio Codec, 2023 (http://arxiv.org/abs/2305.02765).
"""
def __init__(self, num_groups: int, num_levels_per_group: List[int], **kwargs):
super().__init__()
self.num_groups = num_groups
self.codebook_dim_per_group = len(num_levels_per_group)
# Initialize FSQ for each group
self.fsqs = torch.nn.ModuleList(
[FiniteScalarQuantizer(num_levels=num_levels_per_group, **kwargs) for _ in range(self.num_groups)]
)
logging.debug('Initialized %s with', self.__class__.__name__)
logging.debug('\tnum_groups: %d', self.num_groups)
logging.debug('\tcodebook_dim: %d', self.codebook_dim)
logging.debug('\tnum_levels_per_group: %s', num_levels_per_group)
logging.debug('\tcodebook_dim_per_group: %d', self.codebook_dim_per_group)
@property
def num_codebooks(self):
"""Returns the number of codebooks."""
return self.num_groups
@property
def codebook_size(self):
"""Returns the size of the codebook for each group."""
return self.fsqs[0].codebook_size
@property
def codebook_dim(self):
"""Input vector dimension."""
return self.codebook_dim_per_group * self.num_groups
@typecheck()
def forward(self, inputs, input_len):
"""Quantize each group separately, then concatenate the results."""
inputs_grouped = inputs.chunk(self.num_groups, dim=1)
dequantized, indices = [], []
for in_group, fsq_group in zip(inputs_grouped, self.fsqs):
dequantized_group, indices_group = fsq_group(inputs=in_group, input_len=input_len)
dequantized.append(dequantized_group)
indices.append(indices_group)
# concatenate along the feature dimension
dequantized = torch.cat(dequantized, dim=1)
# concatente along the codebook dimension
indices = torch.cat(indices, dim=0)
return dequantized, indices
@typecheck(
input_types={
"inputs": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
"input_len": NeuralType(tuple('B'), LengthsType()),
},
output_types={"indices": NeuralType(('D', 'B', 'T'), TokenIndex())},
)
def encode(self, inputs: torch.Tensor, input_len: torch.Tensor) -> torch.Tensor:
"""Input is split into groups, each group is encoded separately, then the results are concatenated."""
inputs_grouped = inputs.chunk(self.num_groups, dim=1)
indices = []
for in_group, fsq_group in zip(inputs_grouped, self.fsqs):
indices_group = fsq_group.encode(inputs=in_group, input_len=input_len)
indices.append(indices_group)
# concatenate along the codebook dimension
indices = torch.cat(indices, dim=0)
return indices
@typecheck(
input_types={
"indices": NeuralType(('D', 'B', 'T'), TokenIndex()),
"input_len": NeuralType(tuple('B'), LengthsType()),
},
output_types={
"dequantized": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
},
)
def decode(self, indices: torch.Tensor, input_len: torch.Tensor) -> torch.Tensor:
"""Input indices are split into groups, each group is decoded separately, then the results are concatenated."""
indices_grouped = indices.chunk(self.num_groups, dim=0)
dequantized = []
for indices_group, fsq_group in zip(indices_grouped, self.fsqs):
dequantized_group = fsq_group.decode(indices=indices_group, input_len=input_len)
dequantized.append(dequantized_group)
# concatenate along the feature dimension
dequantized = torch.cat(dequantized, dim=1)
return dequantized
@typecheck(
input_types={
"codes": NeuralType(('B', 'D', 'T'), EncodedRepresentation()),
"input_len": NeuralType(tuple('B'), LengthsType()),
},
output_types={
"indices": NeuralType(('B', 'D', 'T'), TokenIndex()),
},
)
def codes_to_indices(self, codes: torch.Tensor, input_len: torch.Tensor) -> torch.Tensor:
"""Converts a code vector to indices."""
codes_rearrange = rearrange(codes, 'B D T -> D B T')
codes_grouped = codes_rearrange.chunk(self.num_groups, dim=0)
indices = []
for codes_group, fsq_group in zip(codes_grouped, self.fsqs):
codes_group_rearrange = rearrange(codes_group, 'D B T -> B D T')
# [B, T]
indices_group = fsq_group.codes_to_indices(codes=codes_group_rearrange)
indices_group = mask_sequence_tensor(indices_group, input_len)
indices.append(indices_group)
# concatenate along the feature dimension
indices = torch.stack(indices, dim=1)
return indices
class ResidualBlock(NeuralModule):
"""
The residual block structure defined by the HiFi-GAN V1 and V2 configurations.
Args:
channels: Input dimension.
filters: Number of channels in the residual convolutions.
kernel_size: Kernel size of the residual convolutions.
dilation: Dilation of the residual convolutions.
dropout_rate: Dropout to apply to residuals.
activation: Activation to apply in between residual convolutions.
"""
def __init__(
self,
channels: int,
filters: int,
kernel_size: int = 3,
dilation: int = 1,
dropout_rate: float = 0.0,
activation: str = "lrelu",
is_causal: bool = False,
pad_mode: str = "reflect",
):
super(ResidualBlock, self).__init__()
self.input_activation = CodecActivation(activation=activation, channels=channels)
self.skip_activation = CodecActivation(activation=activation, channels=filters)
self.dropout = torch.nn.Dropout(dropout_rate)
if not is_causal:
self.input_conv = Conv1dNorm(
in_channels=channels,
out_channels=filters,
kernel_size=kernel_size,
dilation=dilation,
pad_mode=pad_mode,
)
self.skip_conv = Conv1dNorm(
in_channels=filters, out_channels=channels, kernel_size=kernel_size, pad_mode=pad_mode
)
else:
self.input_conv = CausalConv1dNorm(
in_channels=channels,
out_channels=filters,
kernel_size=kernel_size,
dilation=dilation,
pad_mode=pad_mode,
)
self.skip_conv = CausalConv1dNorm(
in_channels=filters, out_channels=channels, kernel_size=kernel_size, pad_mode=pad_mode
)
def remove_weight_norm(self):
self.input_conv.remove_weight_norm()
self.skip_conv.remove_weight_norm()
@property
def input_types(self):
return {"inputs": NeuralType(('B', 'C', 'T'), VoidType()), "input_len": NeuralType(tuple('B'), LengthsType())}
@property
def output_types(self):
return {"out": NeuralType(('B', 'C', 'T'), EncodedRepresentation())}
@typecheck()
def forward(self, inputs, input_len):
conv_input = self.input_activation(inputs)
skip_input = self.input_conv(inputs=conv_input, input_len=input_len)
skip_input = self.skip_activation(skip_input)
res = self.skip_conv(inputs=skip_input, input_len=input_len)
res = self.dropout(res)
out = inputs + res
return out
class ResidualBlockV2(NeuralModule):
"""
Residual block which applies activation to output instead of input.
Args:
channels: Input dimension.
filters: Number of channels in the residual convolutions.
kernel_size: Kernel size of the residual convolutions.
activation: Activation to apply in between residual convolutions.
is_causal: Whether to use causal convolutions.
pad_mode: Type of padding to use for conv1d layers.
See https://docs.pytorch.org/docs/stable/generated/torch.nn.Conv1d.html
"""
def __init__(
self,
channels: int,
filters: int,
kernel_size: int = 3,
activation: str = "lrelu",
is_causal: bool = False,
pad_mode: str = "reflect",
):
super(ResidualBlockV2, self).__init__()
if not is_causal:
self.input_conv = Conv1dNorm(
in_channels=channels,
out_channels=filters,
kernel_size=kernel_size,
activation=activation,
pad_mode=pad_mode,
)
self.skip_conv = Conv1dNorm(
in_channels=filters, out_channels=channels, kernel_size=kernel_size, pad_mode=pad_mode
)
else:
self.input_conv = CausalConv1dNorm(
in_channels=channels,
out_channels=filters,
kernel_size=kernel_size,
activation=activation,
pad_mode=pad_mode,
)
self.skip_conv = CausalConv1dNorm(
in_channels=filters, out_channels=channels, kernel_size=kernel_size, pad_mode=pad_mode
)
self.output_activation = CodecActivation(activation=activation, channels=channels)
def remove_weight_norm(self):
self.input_conv.remove_weight_norm()
self.skip_conv.remove_weight_norm()
@property
def input_types(self):
return {"inputs": NeuralType(('B', 'C', 'T'), VoidType()), "input_len": NeuralType(tuple('B'), LengthsType())}
@property
def output_types(self):
return {"out": NeuralType(('B', 'C', 'T'), EncodedRepresentation())}
@typecheck()
def forward(self, inputs, input_len):
res = self.input_conv(inputs=inputs, input_len=input_len)
res = self.skip_conv(inputs=res, input_len=input_len)
out = inputs + res
out = self.output_activation(out)
out = mask_sequence_tensor(out, lengths=input_len)
return out
class HiFiGANResBlock(NeuralModule):
"""
Residual block wrapper for HiFi-GAN which creates a block for multiple dilations.
Args:
channels: Input dimension.
kernel_size: Kernel size of the residual blocks.
dilations: List of dilations. One residual block will be created for each dilation in the list.
activation: Activation for the residual blocks.
"""
def __init__(
self,
channels: int,
kernel_size: int,
dilations: Iterable[int],
activation: str,
is_causal: bool = False,
pad_mode: str = "reflect",
):
super().__init__()
self.res_blocks = nn.ModuleList(
[
ResidualBlock(
channels=channels,
filters=channels,
kernel_size=kernel_size,
dilation=dilation,
activation=activation,
is_causal=is_causal,
pad_mode=pad_mode,
)
for dilation in dilations
]
)
def remove_weight_norm(self):
for res_block in self.res_blocks:
res_block.remove_weight_norm()
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'C', 'T'), VoidType()),
"input_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {"out": NeuralType(('B', 'C', 'T'), VoidType())}
@typecheck()
def forward(self, inputs, input_len):
out = inputs
for res_block in self.res_blocks:
out = res_block(inputs=out, input_len=input_len)
return out
class HiFiGANResLayer(NeuralModule):
"""
Residual block wrapper for HiFi-GAN which creates a block for multiple kernel sizes and dilations.
One residual block is created for each combination of kernel size and dilation.
Args:
channels: Input dimension.
kernel_sizes: List of kernel sizes.
dilations: List of dilations.
activation: Activation for the residual layers.
"""
def __init__(
self,
channels: int,
kernel_sizes: Iterable[int],
dilations: Iterable[int],
activation: str,
is_causal: bool = False,
pad_mode: str = "reflect",
):
super().__init__()
self.res_blocks = nn.ModuleList(
[
HiFiGANResBlock(
channels=channels,
kernel_size=kernel_size,
dilations=dilations,
activation=activation,
is_causal=is_causal,
pad_mode=pad_mode,
)
for kernel_size in kernel_sizes
]
)
def remove_weight_norm(self):
for res_block in self.res_blocks:
res_block.remove_weight_norm()
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'D', 'T'), VoidType()),
"input_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {"out": NeuralType(('B', 'D', 'T'), VoidType())}
@typecheck()
def forward(self, inputs, input_len):
residuals = [res_block(inputs=inputs, input_len=input_len) for res_block in self.res_blocks]
out = sum(residuals) / len(residuals)
return out
class CausalHiFiGANEncoder(NeuralModule):
"""
Causal Audio encoder created by inverting the HiFi-GAN decoder and replacing Conv1D by CausalConv1D.
Args:
encoded_dim: Dimension of encoder output.
down_sample_rates: Rate to upsample for each decoder block. The product of the downsample rates will
determine the output token rate. For example 2 * 2 * 8 * 8 = 256 samples per token.
base_channels: Number of filters in the first convolution. The number of channels will be doubled after each
downsample layer.
in_kernel_size: Kernel size of the input convolution.
out_kernel_size: Kernel size of the output convolution.
resblock_kernel_sizes: List of kernel sizes to use in each residual block.
resblock_dilation_sizes: List of dilations to use in each residual block.
activation: Activation to use in residual and downsample layers, defaults to leaky relu.
"""
def __init__(
self,
encoded_dim: int,
down_sample_rates: Iterable[int] = (2, 2, 8, 8),
base_channels: int = 32,
in_kernel_size: int = 7,
out_kernel_size: int = 7,
resblock_kernel_sizes: Iterable[int] = (3, 7, 11),
resblock_dilation_sizes: Iterable[int] = (1, 3, 5),
activation: str = "lrelu",
pad_mode: str = "zeros",
):
assert in_kernel_size > 0
assert out_kernel_size > 0
super().__init__()
self.down_sample_rates = down_sample_rates
self.pre_conv = CausalConv1dNorm(
in_channels=1, out_channels=base_channels, kernel_size=in_kernel_size, pad_mode=pad_mode
)
in_channels = base_channels
self.activations = nn.ModuleList([])
self.down_sample_conv_layers = nn.ModuleList([])
self.res_layers = nn.ModuleList([])
for i, down_sample_rate in enumerate(self.down_sample_rates):
res_layer = HiFiGANResLayer(
channels=in_channels,
kernel_sizes=resblock_kernel_sizes,
dilations=resblock_dilation_sizes,
activation=activation,
is_causal=True,
pad_mode=pad_mode,
)
self.res_layers.append(res_layer)
act = CodecActivation(activation, channels=in_channels)
self.activations.append(act)
out_channels = 2 * in_channels
kernel_size = 2 * down_sample_rate
# padding = get_down_sample_padding(kernel_size=kernel_size, stride=down_sample_rate)
down_sample_conv = CausalConv1dNorm(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=down_sample_rate,
pad_mode=pad_mode,
)
in_channels = out_channels
self.down_sample_conv_layers.append(down_sample_conv)
self.post_activation = CodecActivation(activation, channels=in_channels)
self.post_conv = CausalConv1dNorm(
in_channels=in_channels, out_channels=encoded_dim, kernel_size=out_kernel_size, pad_mode=pad_mode
)
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"encoded": NeuralType(('B', 'D', 'T_encoded'), EncodedRepresentation()),
"encoded_len": NeuralType(tuple('B'), LengthsType()),
}
def remove_weight_norm(self):
self.pre_conv.remove_weight_norm()
self.post_conv.remove_weight_norm()
for res_layer in self.res_layers:
res_layer.remove_weight_norm()
for down_sample_conv in self.down_sample_conv_layers:
down_sample_conv.remove_weight_norm()
@typecheck()
def forward(self, audio, audio_len):
encoded_len = audio_len
audio = rearrange(audio, "B T -> B 1 T")
# [B, C, T_audio]
out = self.pre_conv(inputs=audio, input_len=encoded_len)
for act, res_layer, down_sample_conv, down_sample_rate in zip(
self.activations, self.res_layers, self.down_sample_conv_layers, self.down_sample_rates
):
# [B, C, T]
out = res_layer(inputs=out, input_len=encoded_len)
out = act(out)
with default_precision(torch.float32):
encoded_len = (encoded_len // down_sample_rate).long()
# [B, 2 * C, T / down_sample_rate]
out = down_sample_conv(inputs=out, input_len=encoded_len)
out = self.post_activation(out)
# [B, encoded_dim, T_encoded]
encoded = self.post_conv(inputs=out, input_len=encoded_len)
return encoded, encoded_len
class HiFiGANEncoder(NeuralModule):
"""
Audio encoder created by inverting the HiFi-GAN decoder.
Args:
encoded_dim: Dimension of encoder output.
down_sample_rates: Rate to upsample for each decoder block. The product of the downsample rates will
determine the output token rate. For example 2 * 2 * 8 * 8 = 256 samples per token.
base_channels: Number of filters in the first convolution. The number of channels will be doubled after each
downsample layer.
in_kernel_size: Kernel size of the input convolution.
out_kernel_size: Kernel size of the output convolution.
resblock_kernel_sizes: List of kernel sizes to use in each residual block.
resblock_dilation_sizes: List of dilations to use in each residual block.
activation: Activation to use in residual and downsample layers, defaults to leaky relu.
"""
def __init__(
self,
encoded_dim: int,
down_sample_rates: Iterable[int] = (2, 2, 8, 8),
base_channels: int = 32,
in_kernel_size: int = 7,
out_kernel_size: int = 7,
resblock_kernel_sizes: Iterable[int] = (3, 7, 11),
resblock_dilation_sizes: Iterable[int] = (1, 3, 5),
activation: str = "lrelu",
pad_mode: str = "reflect",
):
assert in_kernel_size > 0
assert out_kernel_size > 0
super().__init__()
self.down_sample_rates = down_sample_rates
self.pre_conv = Conv1dNorm(
in_channels=1, out_channels=base_channels, kernel_size=in_kernel_size, pad_mode=pad_mode
)
in_channels = base_channels
self.activations = nn.ModuleList([])
self.down_sample_conv_layers = nn.ModuleList([])
self.res_layers = nn.ModuleList([])
for i, down_sample_rate in enumerate(self.down_sample_rates):
res_layer = HiFiGANResLayer(
channels=in_channels,
kernel_sizes=resblock_kernel_sizes,
dilations=resblock_dilation_sizes,
activation=activation,
pad_mode=pad_mode,
)
self.res_layers.append(res_layer)
act = CodecActivation(activation, channels=in_channels)
self.activations.append(act)
out_channels = 2 * in_channels
kernel_size = 2 * down_sample_rate
padding = get_down_sample_padding(kernel_size=kernel_size, stride=down_sample_rate)
down_sample_conv = Conv1dNorm(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=down_sample_rate,
padding=padding,
pad_mode=pad_mode,
)
in_channels = out_channels
self.down_sample_conv_layers.append(down_sample_conv)
self.post_activation = CodecActivation(activation, channels=in_channels)
self.post_conv = Conv1dNorm(
in_channels=in_channels, out_channels=encoded_dim, kernel_size=out_kernel_size, pad_mode=pad_mode
)
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"encoded": NeuralType(('B', 'D', 'T_encoded'), EncodedRepresentation()),
"encoded_len": NeuralType(tuple('B'), LengthsType()),
}
def remove_weight_norm(self):
self.pre_conv.remove_weight_norm()
self.post_conv.remove_weight_norm()
for res_layer in self.res_layers:
res_layer.remove_weight_norm()
for down_sample_conv in self.down_sample_conv_layers:
down_sample_conv.remove_weight_norm()
@typecheck()
def forward(self, audio, audio_len):
encoded_len = audio_len
audio = rearrange(audio, "B T -> B 1 T")
# [B, C, T_audio]
out = self.pre_conv(inputs=audio, input_len=encoded_len)
for act, res_layer, down_sample_conv, down_sample_rate in zip(
self.activations, self.res_layers, self.down_sample_conv_layers, self.down_sample_rates
):
# [B, C, T]
out = res_layer(inputs=out, input_len=encoded_len)
out = act(out)
with default_precision(torch.float32):
encoded_len = (encoded_len // down_sample_rate).long()
# [B, 2 * C, T / down_sample_rate]
out = down_sample_conv(inputs=out, input_len=encoded_len)
out = self.post_activation(out)
# [B, encoded_dim, T_encoded]
encoded = self.post_conv(inputs=out, input_len=encoded_len)
return encoded, encoded_len
class CausalHiFiGANDecoder(NeuralModule):
"""
Codec decoder using the HiFi-GAN generator architecture with Causal Convolutions.
Args:
input_dim: Input dimension.
up_sample_rates: Rate to upsample for each decoder block. The product of the upsample rates should be the same
as the overall downsample rate for your encoder. For example, a symmetric encoder/decoder can be created
with encoder downsample rates [2, 2, 8, 8] and decoder upsample rates [8, 8, 2, 2].
base_channels: Number of filters in the first convolution. The number of channels will be cut in
half after each upsample layer.
in_kernel_size: Kernel size of the input convolution.
out_kernel_size: Kernel size of the output convolution.
resblock_kernel_sizes: List of kernel sizes to use in each residual block.
resblock_dilation_sizes: List of dilations to use in each residual block.
activation: Activation to use in residual and upsample layers, defaults to leaky relu.
output_activation: Activation to apply to output. To produce a valid audio signal, it should output values in
the range [-1.0, 1.0]. Supports "tanh" and "clamp".
"""
def __init__(
self,
input_dim: int,
up_sample_rates: Iterable[int] = (8, 8, 2, 2),
base_channels: int = 512,
in_kernel_size: int = 7,
out_kernel_size: int = 3,
resblock_kernel_sizes: Iterable[int] = (3, 7, 11),
resblock_dilation_sizes: Iterable[int] = (1, 3, 5),
activation: str = "lrelu",
output_activation: str = "tanh",
pad_mode: str = "zeros",
n_groups_equal_to_out_channels: bool = True,
):
assert in_kernel_size > 0
assert out_kernel_size > 0
super().__init__()
self.up_sample_rates = up_sample_rates
self.pre_conv = CausalConv1dNorm(
in_channels=input_dim, out_channels=base_channels, kernel_size=in_kernel_size, pad_mode=pad_mode
)
in_channels = base_channels
self.activations = nn.ModuleList([])
self.up_sample_conv_layers = nn.ModuleList([])
self.res_layers = nn.ModuleList([])
for i, up_sample_rate in enumerate(self.up_sample_rates):
out_channels = in_channels // 2
kernel_size = 2 * up_sample_rate
act = CodecActivation(activation, channels=in_channels)
self.activations.append(act)
up_sample_conv = CausalConvTranspose1dNorm(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=up_sample_rate,
groups=out_channels if n_groups_equal_to_out_channels else 1,
)
in_channels = out_channels
self.up_sample_conv_layers.append(up_sample_conv)
res_layer = HiFiGANResLayer(
channels=in_channels,
kernel_sizes=resblock_kernel_sizes,
dilations=resblock_dilation_sizes,
activation=activation,
is_causal=True,
pad_mode=pad_mode,
)
self.res_layers.append(res_layer)
self.post_activation = CodecActivation(activation, channels=in_channels)
self.post_conv = CausalConv1dNorm(
in_channels=in_channels, out_channels=1, kernel_size=out_kernel_size, pad_mode=pad_mode
)
if output_activation == "tanh":
self.out_activation = nn.Tanh()
elif output_activation == "clamp":
self.out_activation = ClampActivation()
else:
raise ValueError(f"Invalid audio output activation {output_activation}")
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'D', 'T_encoded'), VoidType()),
"input_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
def remove_weight_norm(self):
self.pre_conv.remove_weight_norm()
for up_sample_conv in self.up_sample_conv_layers:
up_sample_conv.remove_weight_norm()
for res_layer in self.res_layers:
res_layer.remove_weight_norm()
@typecheck()
def forward(self, inputs, input_len):
audio_len = input_len
# [B, C, T_encoded]
out = self.pre_conv(inputs=inputs, input_len=audio_len)
for act, res_layer, up_sample_conv, up_sample_rate in zip(
self.activations, self.res_layers, self.up_sample_conv_layers, self.up_sample_rates
):
with default_precision(torch.float32):
audio_len = (audio_len * up_sample_rate).long()
out = act(out)
# [B, C / 2, T * up_sample_rate]
out = up_sample_conv(inputs=out, input_len=audio_len)
out = res_layer(inputs=out, input_len=audio_len)
out = self.post_activation(out)
# [B, 1, T_audio]
out = self.post_conv(inputs=out, input_len=audio_len)
audio = self.out_activation(out)
audio = rearrange(audio, "B 1 T -> B T")
return audio, audio_len
class HiFiGANDecoder(NeuralModule):
"""
Codec decoder using the HiFi-GAN generator architecture.
Default parameters match the HiFi-GAN V1 configuration for 22.05khz.
Args:
input_dim: Input dimension.
up_sample_rates: Rate to upsample for each decoder block. The product of the upsample rates should be the same
as the overall downsample rate for your encoder. For example, a symmetric encoder/decoder can be created
with encoder downsample rates [2, 2, 8, 8] and decoder upsample rates [8, 8, 2, 2].
base_channels: Number of filters in the first convolution. The number of channels will be cut in
half after each upsample layer.
in_kernel_size: Kernel size of the input convolution.
out_kernel_size: Kernel size of the output convolution.
resblock_kernel_sizes: List of kernel sizes to use in each residual block.
resblock_dilation_sizes: List of dilations to use in each residual block.
activation: Activation to use in residual and upsample layers, defaults to leaky relu.
output_activation: Activation to apply to output. To produce a valid audio signal, it should output values in
the range [-1.0, 1.0]. Supports "tanh" and "clamp".
"""
def __init__(
self,
input_dim: int,
up_sample_rates: Iterable[int] = (8, 8, 2, 2),
base_channels: int = 512,
in_kernel_size: int = 7,
out_kernel_size: int = 3,
resblock_kernel_sizes: Iterable[int] = (3, 7, 11),
resblock_dilation_sizes: Iterable[int] = (1, 3, 5),
activation: str = "lrelu",
output_activation: str = "tanh",
pad_mode: str = "reflect",
n_groups_equal_to_out_channels: bool = False,
):
assert in_kernel_size > 0
assert out_kernel_size > 0
super().__init__()
self.up_sample_rates = up_sample_rates
self.pre_conv = Conv1dNorm(
in_channels=input_dim, out_channels=base_channels, kernel_size=in_kernel_size, pad_mode=pad_mode
)
in_channels = base_channels
self.activations = nn.ModuleList([])
self.up_sample_conv_layers = nn.ModuleList([])
self.res_layers = nn.ModuleList([])
for i, up_sample_rate in enumerate(self.up_sample_rates):
out_channels = in_channels // 2
kernel_size = 2 * up_sample_rate
act = CodecActivation(activation, channels=in_channels)
self.activations.append(act)
up_sample_conv = ConvTranspose1dNorm(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=up_sample_rate,
groups=out_channels if n_groups_equal_to_out_channels else 1,
)
in_channels = out_channels
self.up_sample_conv_layers.append(up_sample_conv)
res_layer = HiFiGANResLayer(
channels=in_channels,
kernel_sizes=resblock_kernel_sizes,
dilations=resblock_dilation_sizes,
activation=activation,
pad_mode=pad_mode,
)
self.res_layers.append(res_layer)
self.post_activation = CodecActivation(activation, channels=in_channels)
self.post_conv = Conv1dNorm(
in_channels=in_channels, out_channels=1, kernel_size=out_kernel_size, pad_mode=pad_mode
)
if output_activation == "tanh":
self.out_activation = nn.Tanh()
elif output_activation == "clamp":
self.out_activation = ClampActivation()
else:
raise ValueError(f"Invalid audio output activation {output_activation}")
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'D', 'T_encoded'), VoidType()),
"input_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
def remove_weight_norm(self):
self.pre_conv.remove_weight_norm()
for up_sample_conv in self.up_sample_conv_layers:
up_sample_conv.remove_weight_norm()
for res_layer in self.res_layers:
res_layer.remove_weight_norm()
@typecheck()
def forward(self, inputs, input_len):
audio_len = input_len
# [B, C, T_encoded]
out = self.pre_conv(inputs=inputs, input_len=audio_len)
for act, res_layer, up_sample_conv, up_sample_rate in zip(
self.activations, self.res_layers, self.up_sample_conv_layers, self.up_sample_rates
):
audio_len = audio_len * up_sample_rate
out = act(out)
# [B, C / 2, T * up_sample_rate]
out = up_sample_conv(inputs=out, input_len=audio_len)
out = res_layer(inputs=out, input_len=audio_len)
out = self.post_activation(out)
# [B, 1, T_audio]
out = self.post_conv(inputs=out, input_len=audio_len)
audio = self.out_activation(out)
audio = rearrange(audio, "B 1 T -> B T")
return audio, audio_len
class MelSpectrogramProcessor(NeuralModule):
"""
Wrapper interface for computing mel spectrogram for codec training.
"""
def __init__(self, sample_rate: int, win_length: int, hop_length: int, mel_dim: int = 80, log_guard: float = 1.0):
super(MelSpectrogramProcessor, self).__init__()
self.mel_dim = mel_dim
self.hop_length = hop_length
self.preprocessor = AudioToMelSpectrogramPreprocessor(
sample_rate=sample_rate,
highfreq=None,
features=mel_dim,
pad_to=1,
exact_pad=True,
n_window_size=win_length,
n_window_stride=hop_length,
window_size=False,
window_stride=False,
n_fft=win_length,
mag_power=1.0,
log=True,
log_zero_guard_type="add",
log_zero_guard_value=log_guard,
mel_norm=None,
normalize=None,
preemph=None,
dither=0.0,
)
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"spec": NeuralType(('B', 'D', 'T_spec'), MelSpectrogramType()),
"spec_len": NeuralType(tuple('B'), LengthsType()),
}
@typecheck()
def forward(self, audio, audio_len):
spec, spec_len = self.preprocessor(input_signal=audio, length=audio_len)
return spec, spec_len
class STFTProcessor(NeuralModule):
"""
Interface for computing log magnitude STFT features.
Args:
n_fft: Size of Fourier transform
win_length: The size of the sliding window frames for windowing and STFT.
hop_length: The distance between neighboring sliding window frames
log_guard: Value to add to magnitude STFT before taking log.
"""
def __init__(self, n_fft, win_length, hop_length, log_guard=1.0, pad_mode="reflect"):
super(STFTProcessor, self).__init__()
self.n_fft = n_fft
self.win_length = win_length
self.hop_length = hop_length
self.register_buffer("window", torch.hann_window(self.win_length, periodic=False))
self.log_guard = log_guard
self.stft_pad_amount = (self.n_fft - self.hop_length) // 2
self.pad_mode = pad_mode
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"spec": NeuralType(('B', 'D', 'T_spec'), MelSpectrogramType()),
"spec_len": NeuralType(tuple('B'), LengthsType()),
}
@typecheck()
def forward(self, audio, audio_len):
spec_len = audio_len // self.hop_length
audio_padded = torch.nn.functional.pad(audio, (self.stft_pad_amount, self.stft_pad_amount), self.pad_mode)
# [B, n_fft, T_spec]
fft = torch.stft(
audio_padded,
n_fft=self.n_fft,
hop_length=self.hop_length,
win_length=self.win_length,
window=self.window,
return_complex=True,
center=False,
)
fft_mag = torch.abs(fft)
fft_mag_log = torch.log(fft_mag + self.log_guard)
fft_mag_log = mask_sequence_tensor(fft_mag_log, spec_len)
return fft_mag_log, spec_len
class ResNetEncoder(NeuralModule):
"""
Residual network which uses HiFi-GAN residual blocks to encode spectrogram features without changing
the time dimension.
Args:
in_channels: input dimension
out_channels: output dimension
num_layers: number of residual blocks to use
hidden_channels: encoder hidden dimension
filters: number of filters in residual block layers
kernel_size: kernel size in residual block convolutions
dropout_rate: Optional dropout rate to apply to residuals.
activation: Activation to use, defaults to leaky relu.
"""
def __init__(
self,
in_channels: int,
out_channels: int,
num_layers: int = 6,
hidden_channels: int = 256,
filters: int = 768,
kernel_size: int = 3,
dropout_rate: float = 0.1,
activation: str = "lrelu",
pad_mode: str = "reflect",
):
super(ResNetEncoder, self).__init__()
self.pre_conv = Conv1dNorm(
in_channels=in_channels, out_channels=hidden_channels, kernel_size=kernel_size, pad_mode=pad_mode
)
self.res_layers = nn.ModuleList(
[
ResidualBlock(
channels=hidden_channels,
filters=filters,
kernel_size=kernel_size,
dropout_rate=dropout_rate,
activation=activation,
pad_mode=pad_mode,
)
for _ in range(num_layers)
]
)
self.post_activation = CodecActivation(activation, channels=hidden_channels)
self.post_conv = Conv1dNorm(
in_channels=hidden_channels, out_channels=out_channels, kernel_size=kernel_size, pad_mode=pad_mode
)
def remove_weight_norm(self):
self.pre_conv.remove_weight_norm()
self.post_conv.remove_weight_norm()
for res_layer in self.res_layers:
res_layer.remove_weight_norm()
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'D', 'T'), VoidType()),
"input_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {"encoded": NeuralType(('B', 'C', 'T'), EncodedRepresentation())}
@typecheck()
def forward(self, inputs, input_len):
encoded = self.pre_conv(inputs=inputs, input_len=input_len)
for res_layer in self.res_layers:
encoded = res_layer(inputs=encoded, input_len=input_len)
encoded = self.post_activation(encoded)
encoded = self.post_conv(inputs=encoded, input_len=input_len)
return encoded
class FullBandMelEncoder(NeuralModule):
"""
Encoder which encodes the entire mel spectrogram with a single encoder network.
Args:
mel_processor: MelSpectrogramProcessor or equivalent class instance for computing the mel spectrogram from
input audio.
encoder: ResNetEncoder or equivalent class for encoding the mel spectrogram.
"""
def __init__(self, mel_processor: NeuralModule, encoder: NeuralModule):
super(FullBandMelEncoder, self).__init__()
self.mel_processor = mel_processor
self.encoder = encoder
def remove_weight_norm(self):
self.encoder.remove_weight_norm()
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"encoded": NeuralType(('B', 'C', 'T_encoded'), EncodedRepresentation()),
"encoded_len": NeuralType(tuple('B'), LengthsType()),
}
@typecheck()
def forward(self, audio, audio_len):
out, spec_len = self.mel_processor(audio=audio, audio_len=audio_len)
encoded = self.encoder(inputs=out, input_len=spec_len)
return encoded, spec_len
class MultiBandMelEncoder(NeuralModule):
"""
Encoder which splits mel spectrogram into bands and encodes each using separate residual networks.
Args:
mel_bands: List of mel spectrogram bands to encode.
Each list element is tuple of 2 elements with the start and end index of the mel features to use.
mel_processor: MelSpectrogramProcessor or equivalent class instance for computing the mel spectrogram from
input audio.
encoder_kwargs: Arguments for constructing encoder for each mel band.
"""
def __init__(self, mel_bands: Iterable[Tuple[int, int]], mel_processor: NeuralModule, **encoder_kwargs):
super(MultiBandMelEncoder, self).__init__()
self.validate_mel_bands(mel_dim=mel_processor.mel_dim, mel_bands=mel_bands)
self.mel_bands = mel_bands
self.mel_processor = mel_processor
band_dims = [band[1] - band[0] for band in self.mel_bands]
self.encoders = nn.ModuleList(
[ResNetEncoder(in_channels=band_dim, **encoder_kwargs) for band_dim in band_dims]
)
@staticmethod
def validate_mel_bands(mel_dim: int, mel_bands: Iterable[Tuple[int, int]]):
mel_dims_used = np.zeros([mel_dim], dtype=bool)
for band in mel_bands:
mel_dims_used[band[0] : band[1]] = True
if not all(mel_dims_used):
missing_dims = np.where(~mel_dims_used)
raise ValueError(f"Mel bands must cover all {mel_dim} dimensions. Missing {missing_dims}.")
return
def remove_weight_norm(self):
for encoder in self.encoders:
encoder.remove_weight_norm()
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"encoded": NeuralType(('B', 'C', 'T_encoded'), EncodedRepresentation()),
"encoded_len": NeuralType(tuple('B'), LengthsType()),
}
@typecheck()
def forward(self, audio, audio_len):
spec, spec_len = self.mel_processor(audio=audio, audio_len=audio_len)
outputs = []
for (band_start, band_end), encoder in zip(self.mel_bands, self.encoders):
# [B, D_band, T]
spec_band = spec[:, band_start:band_end, :]
band_out = encoder(inputs=spec_band, input_len=spec_len)
outputs.append(band_out)
# [B, C, T]
encoded = torch.cat(outputs, dim=1)
return encoded, spec_len
class STFTResidualBlock(NeuralModule):
"""
Block in multi-resolution STFT encoder which adds an STFT resolution to the encoder latent space, after down
sampling the input to match the time resoluton of the STFT features.
Args:
resolution: STFT resolution, formatted as a 3-tuple (n_fft, hop_length, window_size)
input_dim: Dimension if input latenct features.
filters: Number of channels in the residual convolutions.
kernel_size: Kernel size of the residual convolutions.
activation: Name of activation function.
down_sample_rate: Down sample factor to reduce input by before adding STFT encoding.
"""
def __init__(
self,
resolution: Tuple[int],
input_dim: int,
filters: int,
kernel_size: int,
activation: str,
down_sample_rate: int,
pad_mode: str,
):
super(STFTResidualBlock, self).__init__()
down_sample_kernel_size = down_sample_rate * 2 + 1
self.down_sample_rate = down_sample_rate
self.down_sample_conv = Conv1dNorm(
in_channels=input_dim,
out_channels=filters,
kernel_size=down_sample_kernel_size,
stride=self.down_sample_rate,
activation=activation,
pad_mode=pad_mode,
)
n_fft, hop_length, win_length = resolution
stft_dim = n_fft // 2 + 1
self.spec_processor = STFTProcessor(
n_fft=n_fft, win_length=win_length, hop_length=hop_length, pad_mode=pad_mode
)
self.spec_conv = Conv1dNorm(
in_channels=stft_dim, out_channels=filters, kernel_size=kernel_size, pad_mode=pad_mode
)
self.spec_act = CodecActivation(activation=activation, channels=filters)
self.res_block = ResidualBlockV2(
channels=filters, filters=filters, kernel_size=kernel_size, activation=activation, pad_mode=pad_mode
)
def remove_weight_norm(self):
self.input_conv.remove_weight_norm()
self.skip_conv.remove_weight_norm()
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'C', 'T'), VoidType()),
"input_len": NeuralType(tuple('B'), LengthsType()),
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"out": NeuralType(('B', 'C', 'T'), EncodedRepresentation()),
"out_len": NeuralType(tuple('B'), LengthsType()),
}
@typecheck()
def forward(self, inputs, input_len, audio, audio_len):
out_len = input_len // self.down_sample_rate
out = self.down_sample_conv(inputs=inputs, input_len=out_len)
spec, _ = self.spec_processor(audio=audio, audio_len=audio_len)
spec_res = self.spec_conv(inputs=spec, input_len=out_len)
out = out + spec_res
out = self.spec_act(out)
out = self.res_block(inputs=out, input_len=out_len)
return out, out_len
class DownSampleResidualBlock(NeuralModule):
"""
Layer which combines a down sampling layer with a residual block.
Args:
channels: Input dimension.
filters: Number of channels in the residual convolutions.
kernel_size: Kernel size of the residual convolutions.
activation: Activation to apply in between residual convolutions.
down_sample_rate: Factor to down sample time dimension by.
"""
def __init__(
self,
channels: int,
filters: int,
kernel_size: int,
activation: str,
down_sample_rate: int,
pad_mode: str,
):
super(DownSampleResidualBlock, self).__init__()
down_sample_kernel_size = down_sample_rate * 2 + 1
self.down_sample_rate = down_sample_rate
self.down_sample_conv = Conv1dNorm(
in_channels=channels,
out_channels=filters,
kernel_size=down_sample_kernel_size,
stride=self.down_sample_rate,
activation=activation,
pad_mode=pad_mode,
)
self.res_block = ResidualBlockV2(
channels=filters, filters=filters, kernel_size=kernel_size, activation=activation, pad_mode=pad_mode
)
def remove_weight_norm(self):
self.input_conv.remove_weight_norm()
self.skip_conv.remove_weight_norm()
@property
def input_types(self):
return {"inputs": NeuralType(('B', 'C', 'T'), VoidType()), "input_len": NeuralType(tuple('B'), LengthsType())}
@property
def output_types(self):
return {
"out": NeuralType(('B', 'C', 'T'), EncodedRepresentation()),
"out_len": NeuralType(tuple('B'), LengthsType()),
}
@typecheck()
def forward(self, inputs, input_len):
output_len = input_len // self.down_sample_rate
out = self.down_sample_conv(inputs=inputs, input_len=output_len)
out = self.res_block(inputs=out, input_len=output_len)
return out, output_len
class MultiResolutionSTFTEncoder(NeuralModule):
"""
Encoder which computes log magnitude STFT features at several time resolutions and encodes them into a low
frame-rate representation.
Args:
out_dim: Dimension of encoder output embedding.
resolutions: List of STFT resolutions, formatted as 3-tuples (n_fft, hop_length, window_size)
resolution_filter_list: List the same size as 'resolutions', specifying the number of filters in the residual
block for each STFT resolution.
down_sample_filter_list: List of filters to use for each down sampling block after initial STFT encoding.
down_sample_rate_list: List of rates to use for each down sampling block after initial STFT encoding.
The total down sample rate of the encoder will be 2**(len(resolutions)) * product(down_sample_rate_list)
kernel_size: Kernel size to use in all convolutions.
activation: Name of activation function.
pad_mode: Type of padding to use for conv1d layers.
See https://docs.pytorch.org/docs/stable/generated/torch.nn.Conv1d.html
"""
def __init__(
self,
out_dim: int,
resolutions: List[Tuple[int]],
resolution_filter_list: List[int],
down_sample_filter_list: Tuple[int] = (),
down_sample_rate_list: Tuple[int] = (),
kernel_size: int = 3,
activation: str = "lrelu",
pad_mode: str = "replicate",
):
super(MultiResolutionSTFTEncoder, self).__init__()
assert len(resolutions) >= 1
assert len(resolutions) == len(resolution_filter_list)
n_fft, hop_length, win_length = resolutions[0]
input_filters = resolution_filter_list[0]
input_dim = n_fft // 2 + 1
self.pre_spec_processor = STFTProcessor(
n_fft=n_fft, win_length=win_length, hop_length=hop_length, pad_mode=pad_mode
)
self.pre_conv = Conv1dNorm(
in_channels=input_dim,
out_channels=input_filters,
kernel_size=kernel_size,
activation=activation,
pad_mode=pad_mode,
)
self.pre_res_block = ResidualBlockV2(
channels=input_filters,
filters=input_filters,
kernel_size=kernel_size,
activation=activation,
pad_mode=pad_mode,
)
input_dim = input_filters
self.stft_blocks = nn.ModuleList([])
for resolution, filters in zip(resolutions[1:], resolution_filter_list[1:]):
stft_block = STFTResidualBlock(
resolution=resolution,
input_dim=input_dim,
down_sample_rate=2,
filters=filters,
kernel_size=kernel_size,
activation=activation,
pad_mode=pad_mode,
)
self.stft_blocks.append(stft_block)
input_dim = filters
if down_sample_filter_list and not down_sample_rate_list:
down_sample_rate_list = len(down_sample_filter_list) * [2]
self.down_sample_blocks = nn.ModuleList([])
for filters, down_sample_rate in zip(down_sample_filter_list, down_sample_rate_list):
down_sample_block = DownSampleResidualBlock(
channels=input_dim,
filters=filters,
down_sample_rate=down_sample_rate,
kernel_size=kernel_size,
activation=activation,
pad_mode=pad_mode,
)
self.down_sample_blocks.append(down_sample_block)
input_dim = filters
self.post_conv = Conv1dNorm(
in_channels=input_dim,
out_channels=out_dim,
kernel_size=kernel_size,
pad_mode=pad_mode,
)
def remove_weight_norm(self):
self.encoder.remove_weight_norm()
@property
def input_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"encoded": NeuralType(('B', 'D', 'T_encoded'), EncodedRepresentation()),
"encoded_len": NeuralType(tuple('B'), LengthsType()),
}
@typecheck()
def forward(self, audio, audio_len):
encoded, encoded_len = self.pre_spec_processor(audio=audio, audio_len=audio_len)
encoded = self.pre_conv(inputs=encoded, input_len=encoded_len)
encoded = self.pre_res_block(inputs=encoded, input_len=encoded_len)
for stft_block in self.stft_blocks:
encoded, encoded_len = stft_block(inputs=encoded, input_len=encoded_len, audio=audio, audio_len=audio_len)
for down_sample_block in self.down_sample_blocks:
encoded, encoded_len = down_sample_block(inputs=encoded, input_len=encoded_len)
encoded = self.post_conv(inputs=encoded, input_len=encoded_len)
return encoded, encoded_len
class VectorQuantizerIndexConverter(NeuralModule):
"""
Utility for converting indices between two FSQ definitions.
Example:
from nemo.collections.tts.models import AudioCodecModel
from nemo.collections.tts.modules.audio_codec_modules import GroupFiniteScalarQuantizer, VectorQuantizerIndexConverter
audio_file = "/home/audio.wav"
codec_path = "/home/SpectralCodecFps43.nemo"
device = "cuda:0"
audio, _ = librosa.load(audio_file, sr=sample_rate)
audio_tensor = torch.tensor([audio]).to(device)
audio_len_tensor = torch.tensor([audio.shape[0]]).to(device)
codec_model = AudioCodecModel.restore_from(codec_path, map_location=device)
tokens, token_len = codec_model.encode(audio=audio_tensor, audio_len=audio_len_tensor)
fsq_new = GroupFiniteScalarQuantizer(num_groups=6, num_levels_per_group=[5, 5, 5, 5]).to(device)
# vector_quantizer_original has 4 codebooks with 6 levels [5, 5, 5, 5, 5, 5]
# vector_quantizer_new has 6 codebooks with 4 levels [5, 5, 5, 5]
fsq_converter = VectorQuantizerIndexConverter(
vector_quantizer_original=codec_model.vector_quantizer,
vector_quantizer_new=fsq_new
)
tokens_new = fsq_converter.convert_original_to_new(audio_tokens=tokens, audio_lens=token_len)
tokens_original = fsq_converter.convert_new_to_original(audio_tokens=tokens_new, audio_lens=token_len)
"""
def __init__(self, vector_quantizer_original, vector_quantizer_new):
super().__init__()
self.vector_quantizer_original = vector_quantizer_original
self.vector_quantizer_new = vector_quantizer_new
# Input [batch, num_codebooks_original, time]
# Output [batch, num_codebooks_new, time]
def convert_original_to_new(self, audio_tokens, audio_lens):
audio_tokens_rearrange = rearrange(audio_tokens, 'B C T -> C B T')
audio_codes = self.vector_quantizer_original.decode(indices=audio_tokens_rearrange, input_len=audio_lens)
audio_tokens_new = self.vector_quantizer_new.codes_to_indices(codes=audio_codes, input_len=audio_lens)
return audio_tokens_new
# Input [batch, num_codebooks_new, time]
# Output [batch, num_codebooks_original, time]
def convert_new_to_original(self, audio_tokens, audio_lens):
audio_tokens_rearrange = rearrange(audio_tokens, 'B C T -> C B T')
audio_codes = self.vector_quantizer_new.decode(indices=audio_tokens_rearrange, input_len=audio_lens)
audio_tokens_original = self.vector_quantizer_original.codes_to_indices(
codes=audio_codes, input_len=audio_lens
)
return audio_tokens_original
class ResNetDecoder(NeuralModule):
"""
A residual decoder designed for low-latency. Most processing is done at a low frame-rate (e.g. 50 FPS), while
minimizing the size of the network which upsamples to the final waveform.
Args:
input_dim: Dimension of decoder input.
input_filters: Size of the first CNN layer applied to the decoder input.
pre_up_sample_rates: Up sample rates to apply prior to main decoder network.
pre_up_sample_filters: Size of residual blocks in first up sampling blocks.
n_hidden_layers: Number of residual blocks in the main decoder network, which processes the latent space at
low frame-rate.
hidden_filters: Size of each rsidual block in the main decoder network.
resblock_up_sample_rates: Up sample rates to apply after main decoder network.
resblock_up_sample_filters: Size of residual blocks in final up sampling blocks.
resblock_up_sample_kernel_size: Kernel size to use in final up sampling blocks.
kernel_size: Kernel size to use in all other CNN layers.
activation: Name of activation to use in residual blocks.
is_causal: Whether to make the decoder causal.
pad_mode: Type of padding to use for conv1d layers.
See https://docs.pytorch.org/docs/stable/generated/torch.nn.Conv1d.html
"""
def __init__(
self,
input_dim: int,
input_filters: int,
pre_up_sample_rates: List[int],
pre_up_sample_filters: List[int],
n_hidden_layers: int,
hidden_filters: int,
resblock_up_sample_rates: List[int],
resblock_up_sample_filters: List[int],
resblock_up_sample_kernel_size: int = 7,
kernel_size: int = 3,
activation: str = "half_snake",
is_causal: bool = False,
pad_mode: str = "replicate",
):
super().__init__()
assert len(pre_up_sample_rates) == len(pre_up_sample_filters)
assert len(resblock_up_sample_rates) == len(resblock_up_sample_filters)
if not is_causal:
conv_class = Conv1dNorm
else:
conv_class = CausalConv1dNorm
if not is_causal:
conv_transpose_class = ConvTranspose1dNorm
else:
conv_transpose_class = CausalConvTranspose1dNorm
self.pre_conv = conv_class(
in_channels=input_dim,
out_channels=input_filters,
kernel_size=kernel_size,
)
in_channels = input_filters
self.pre_up_sample_rates = pre_up_sample_rates
self.pre_resblocks = nn.ModuleList([])
self.pre_up_sample_layers = nn.ModuleList([])
for up_sample_rate, filters in zip(self.pre_up_sample_rates, pre_up_sample_filters):
res_block = ResidualBlockV2(
channels=in_channels,
filters=(2 * in_channels),
kernel_size=kernel_size,
activation=activation,
is_causal=is_causal,
pad_mode=pad_mode,
)
self.pre_resblocks.append(res_block)
conv = conv_transpose_class(
in_channels=in_channels,
out_channels=filters,
kernel_size=(2 * up_sample_rate),
stride=up_sample_rate,
activation=activation,
)
self.pre_up_sample_layers.append(conv)
in_channels = filters
self.conv_layers = nn.ModuleList(
[
ResidualBlockV2(
channels=in_channels,
filters=hidden_filters,
kernel_size=kernel_size,
activation=activation,
is_causal=is_causal,
pad_mode=pad_mode,
)
for _ in range(n_hidden_layers)
]
)
self.resblock_up_sample_rates = resblock_up_sample_rates
self.resblock_up_sample_layers = nn.ModuleList([])
self.resblocks = nn.ModuleList([])
for up_sample_rate, filters in zip(self.resblock_up_sample_rates, resblock_up_sample_filters):
conv = conv_transpose_class(
in_channels=in_channels,
out_channels=filters,
kernel_size=(2 * up_sample_rate),
stride=up_sample_rate,
activation=activation,
)
self.resblock_up_sample_layers.append(conv)
res_block = ResidualBlockV2(
channels=filters,
filters=(2 * filters),
kernel_size=resblock_up_sample_kernel_size,
activation=activation,
is_causal=is_causal,
pad_mode=pad_mode,
)
self.resblocks.append(res_block)
in_channels = filters
self.post_conv = conv_class(
in_channels=in_channels, out_channels=1, kernel_size=resblock_up_sample_kernel_size, pad_mode=pad_mode
)
self.out_activation = ClampActivation(clamp_training=False)
@property
def input_types(self):
return {
"inputs": NeuralType(('B', 'D', 'T_encoded'), VoidType()),
"input_len": NeuralType(tuple('B'), LengthsType()),
}
@property
def output_types(self):
return {
"audio": NeuralType(('B', 'T_audio'), AudioSignal()),
"audio_len": NeuralType(tuple('B'), LengthsType()),
}
@typecheck()
def forward(self, inputs, input_len):
out = self.pre_conv(inputs=inputs, input_len=input_len)
audio_len = input_len
for pre_up_sample_rate, pre_up_sample_layer, pre_resblock in zip(
self.pre_up_sample_rates, self.pre_up_sample_layers, self.pre_resblocks
):
out = pre_resblock(inputs=out, input_len=audio_len)
audio_len = pre_up_sample_rate * audio_len
out = pre_up_sample_layer(inputs=out, input_len=audio_len)
for conv in self.conv_layers:
out = conv(inputs=out, input_len=audio_len)
for resblock_up_sample_rate, resblock_up_sample_layer, resblock in zip(
self.resblock_up_sample_rates, self.resblock_up_sample_layers, self.resblocks
):
audio_len = resblock_up_sample_rate * audio_len
out = resblock_up_sample_layer(inputs=out, input_len=audio_len)
out = resblock(inputs=out, input_len=audio_len)
out = self.post_conv(inputs=out, input_len=audio_len)
out = rearrange(out, 'B 1 T -> B T')
audio = self.out_activation(out)
audio = mask_sequence_tensor(audio, audio_len)
return audio, audio_len
|