File size: 144,388 Bytes
5d9f970 | 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 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 | from __future__ import annotations
import hashlib
import contextlib
import importlib.util
import json
import random
import sqlite3
import sys
import wave
from collections import OrderedDict
from pathlib import Path
from types import SimpleNamespace
import numpy as np
import pytest
import torch
import torch.nn.functional as F
from torch import nn
from safetensors.torch import save_file
from psi.config.transform import SeedPosttrainRepackTransform
from psi.config.model_full_duplex_psi0 import FullDuplexPsi0ModelConfig
from psi.data.seed.full_duplex_audio_command_dataset import (
AudioSource,
BackgroundWaveformSource,
CANONICAL_MOTION_MANIFEST_FORMAT,
DISFLUENCY_UNCERTAINTY_ROLES,
DisfluencyAudioSource,
FullDuplexAudioCommandDataset,
FullDuplexTransformedDataset,
HiddenSlice,
IDLE_TOKEN_ID,
LISTEN_TOKEN_ID,
MOTION_TOKEN_ID,
SourceBackgroundAssignment,
StationaryMotionSource,
detect_clean_speech_onset_sample,
has_known_tts_translation_mismatch,
instruction_text_for_voice,
load_canonical_motion_selection,
)
from psi.data.seed.full_duplex_audio_contract import (
AUDIO_COMMAND_STREAMING_PROMPT,
HFD_AUDIO_SOURCE_FORMAT,
HFD_ONLINE_ENCODER_ATTENTION,
HFD_ONLINE_ENCODER_BATCH_SIZE,
HFD_ONLINE_ENCODER_CONTRACT,
HFD_ONLINE_ENCODER_DTYPE,
)
from psi.models.full_duplex_psi0 import FullDuplexPsi0Model, StreamingCausalMemory
from psi.trainers.posttrain_full_duplex import (
PosttrainFullDuplexTrainer,
SourceBatchingLoader,
SourceLengthGroupedSampler,
_source_prefetch_factor,
average_full_duplex_gradients,
)
from psi.data.seed.full_duplex_timing import (
aligned_motion_start_frame,
complete_audio_clip_starts,
)
from psi.data.seed.motion_repr import (
local_delta_from_world,
quat_to_yaw_wxyz,
rpy_to_quat_wxyz,
wrap_angle,
)
from psi.data.seed.source_split import select_source_row_ids
def test_train_and_eval_are_disjoint_at_complete_source_level():
source_ids = ["source-a", "source-a", "source-b", "source-c", "source-c"]
train_rows = select_source_row_ids(
source_ids, split="train", val_ratio=0.25, split_seed=42
)
eval_rows = select_source_row_ids(
source_ids, split="val", val_ratio=0.25, split_seed=42
)
train_sources = {source_ids[index] for index in train_rows}
eval_sources = {source_ids[index] for index in eval_rows}
assert train_sources.isdisjoint(eval_sources)
def test_clean_speech_onset_ignores_leading_silence_and_pcm_click():
rate = 16_000
waveform = np.zeros(rate, dtype=np.float32)
waveform[800] = 0.8
start = 4_000
time_axis = np.arange(rate - start, dtype=np.float32) / rate
waveform[start:] = 0.1 * np.sin(2.0 * np.pi * 220.0 * time_axis)
onset = detect_clean_speech_onset_sample(waveform, rate)
assert start - 320 <= onset <= start + 160
def test_latest_instruction_text_matches_selected_tts_language():
record = {
"row_id": 7,
"instruction": "walk to the left",
"instruction_zh": "向左走",
}
english = AudioSource(
source_key="seed_audio/000007/female_01_en",
clips=(),
)
chinese = AudioSource(
source_key="seed_audio/000007/male_01_zh",
clips=(),
)
assert instruction_text_for_voice(record, english) == "walk to the left"
assert instruction_text_for_voice(record, chinese) == "向左走"
def test_known_retro_translation_mismatch_excludes_only_chinese_voices():
assert has_known_tts_translation_mismatch(
"retro dance jackson jump up",
"后退舞步杰克逊跳起",
)
assert not has_known_tts_translation_mismatch(
"walk backward five steps",
"后退五步",
)
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.audio_catalog = {
"seed_audio/004603": (
AudioSource("seed_audio/004603/female_01_en", ()),
AudioSource("seed_audio/004603/female_01_zh", ()),
AudioSource("seed_audio/004603/male_01_en", ()),
AudioSource("seed_audio/004603/male_01_zh", ()),
)
}
record = {
"row_id": 1,
"audio_dir": "seed_audio/004603",
"_exclude_zh_voices": True,
}
assert dataset._session_voice_names(record) == (
"female_01_en",
"male_01_en",
)
def test_canonical_motion_manifest_loads_exact_bilingual_mapping(tmp_path):
path = tmp_path / "canonical.json"
path.write_text(
json.dumps(
{
"format": CANONICAL_MOTION_MANIFEST_FORMAT,
"excluded_ambiguous_chinese_instructions": [],
"selections": [
{
"instruction": "wave two hands in front",
"instruction_zh": "双手在身前挥动",
"filename": "wave_two_hands_R_001__A533",
},
{
"instruction": "walk forward",
"instruction_zh": "向前走",
"filename": "walk_forward_001__A100",
},
],
},
ensure_ascii=False,
),
encoding="utf-8",
)
assert load_canonical_motion_selection(path) == {
(
"wave two hands in front",
"双手在身前挥动",
): "wave_two_hands_R_001__A533",
("walk forward", "向前走"): "walk_forward_001__A100",
}
def test_canonical_motion_manifest_rejects_duplicate_motion(tmp_path):
path = tmp_path / "canonical.json"
path.write_text(
json.dumps(
{
"format": CANONICAL_MOTION_MANIFEST_FORMAT,
"excluded_ambiguous_chinese_instructions": [],
"selections": [
{
"instruction": "first command",
"instruction_zh": "第一条指令",
"filename": "same_motion",
},
{
"instruction": "second command",
"instruction_zh": "第二条指令",
"filename": "same_motion",
},
],
},
ensure_ascii=False,
),
encoding="utf-8",
)
with pytest.raises(RuntimeError, match="cannot represent two"):
load_canonical_motion_selection(path)
def test_canonical_motion_manifest_requires_ambiguous_chinese_exclusion(
tmp_path,
):
path = tmp_path / "canonical.json"
path.write_text(
json.dumps(
{
"format": CANONICAL_MOTION_MANIFEST_FORMAT,
"excluded_ambiguous_chinese_instructions": [],
"selections": [
{
"instruction": "walk forward",
"instruction_zh": "向前走",
"filename": "walk_forward",
},
{
"instruction": "start walking forward",
"instruction_zh": "向前走",
"filename": "walk_forward_start",
},
],
},
ensure_ascii=False,
),
encoding="utf-8",
)
with pytest.raises(RuntimeError, match="exclude every Chinese prompt"):
load_canonical_motion_selection(path)
def test_multiturn_partition_uses_every_split_record_once():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.multi_turn_sessions = True
dataset.session_min_turns = 2
dataset.session_max_turns = 4
dataset.split = "train"
dataset.split_seed = 42
records = [{"row_id": index} for index in range(23)]
first = dataset._compose_sessions(records)
second = dataset._compose_sessions(records)
assert first == second
assert all(2 <= len(session) <= 4 for session in first)
flattened = [record["row_id"] for session in first for record in session]
assert sorted(flattened) == list(range(23))
assert len(flattened) == len(set(flattened))
def _session_voice_fixture(split: str = "train"):
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.multi_turn_sessions = True
dataset.session_min_turns = 2
dataset.session_max_turns = 4
dataset.session_voice_variants = 2
dataset.split = split
dataset.split_seed = 42
records = [
{"row_id": index, "audio_dir": f"audio-{index}"}
for index in range(32)
]
voice_names = (
"female_01_en",
"female_01_zh",
"male_01_en",
"male_01_zh",
)
dataset.audio_catalog = {
record["audio_dir"]: tuple(
AudioSource(
source_key=f"{record['audio_dir']}/{voice_name}",
clips=(),
packed_path=Path(f"/{record['audio_dir']}/{voice_name}"),
packed_count=voice_index + 2,
)
for voice_index, voice_name in enumerate(voice_names)
)
for record in records
}
return dataset, records
def test_one_to_four_partition_keeps_every_record_and_multiple_turn_lengths():
dataset, _ = _session_voice_fixture()
dataset.session_min_turns = 1
dataset.session_voice_variants = 0
records = [{"row_id": i} for i in range(1000)]
sessions = dataset._compose_sessions(records)
assert sessions == dataset._compose_sessions(records)
assert {len(s) for s in sessions} == {1, 2, 3, 4}
assert sorted(r["row_id"] for s in sessions for r in s) == list(range(1000))
assert dataset._compose_sessions(records[:1]) == ((records[0],),)
@pytest.mark.parametrize("split", ["train", "val"])
def test_single_turn_allowed_but_command_interruption_still_has_two_sources(split):
dataset, records = _session_voice_fixture(split)
dataset.session_min_turns = 1
dataset.interruption_sessions = True
dataset.independent_task_pools = True
dataset.interruption_min_start_seconds = 2.
dataset.interruption_min_remaining_seconds = 1.5
dataset.motion_fps = 50
dataset.task_session_variants = {
"turn-taking": 2, "interruption": 2, "emergency-stop": 2,
"safe-hold": 0, "backchannel": 2,
}
dataset.control_task_session_ratio = .1
dataset.control_followup_ratio = .5
dataset._estimated_runtime_motion_frames = lambda r: 400
dataset._control_motion_is_eligible = lambda *args, **kwargs: True
dataset._backchannel_motion_is_eligible = lambda *args, **kwargs: True
sessions = dataset._compose_sessions(records)
assert sessions == dataset._compose_sessions(records)
for mode, repeats in dataset.task_session_variants.items():
selected = [s for s in sessions if s[0]["_session_interaction_mode"] == mode]
count = repeats if split == "train" else int(repeats > 0)
assert sorted(r["row_id"] for s in selected for r in s) == sorted(list(range(32))*count)
if mode == "interruption":
assert all(2 <= len(s) <= 4 for s in selected)
elif selected:
assert any(len(s) == 1 for s in selected)
for session in selected:
assert len({r["_session_voice_name"] for r in session}) == 1
if mode == "emergency-stop" and len(session) == 1:
assert session[0]["_session_control_turn_index"] == 0
assert not session[0]["_session_control_has_followup"]
def test_singleton_voice_bucket_does_not_require_a_different_speaker():
dataset, records = _session_voice_fixture("val")
dataset.session_min_turns = 1
sessions = dataset._compose_sessions(records[:1])
assert len(sessions) == len(sessions[0]) == 1
assert sessions[0][0]["row_id"] == records[0]["row_id"]
def test_train_session_voice_variants_fix_one_voice_and_expand_each_motion():
dataset, records = _session_voice_fixture("train")
first = dataset._compose_sessions(records)
second = dataset._compose_sessions(records)
assert first == second
assert sum(len(session) for session in first) == 2 * len(records)
row_voices: dict[int, set[str]] = {
int(record["row_id"]): set() for record in records
}
row_neighbours: dict[int, set[tuple[int, ...]]] = {
int(record["row_id"]): set() for record in records
}
for session in first:
assert 2 <= len(session) <= 4
assert len({record["_session_voice_name"] for record in session}) == 1
row_ids = tuple(sorted(int(record["row_id"]) for record in session))
for record in session:
row_id = int(record["row_id"])
row_voices[row_id].add(str(record["_session_voice_name"]))
row_neighbours[row_id].add(row_ids)
selected = dataset._voice_for_record(record)
assert Path(selected.source_key).name == record["_session_voice_name"]
assert all(len(voices) == 2 for voices in row_voices.values())
assert any(len(neighbours) == 2 for neighbours in row_neighbours.values())
def test_validation_uses_one_deterministic_same_voice_session_variant():
dataset, records = _session_voice_fixture("val")
sessions = dataset._compose_sessions(records)
flattened = [record for session in sessions for record in session]
assert len(flattened) == len(records)
assert sorted(int(record["row_id"]) for record in flattened) == list(
range(len(records))
)
assert all(record["_session_voice_variant"] == 0 for record in flattened)
assert all(
len({record["_session_voice_name"] for record in session}) == 1
for session in sessions
)
@pytest.mark.parametrize("split", ["train", "val"])
def test_interruption_composition_is_exactly_one_primitive_use_per_mode(split):
dataset, records = _session_voice_fixture(split)
dataset.interruption_sessions = True
dataset.interruption_ratio = 0.5
dataset.interruption_min_start_seconds = 2.0
dataset.interruption_max_start_seconds = 6.0
dataset.interruption_min_remaining_seconds = 1.5
dataset.motion_fps = 50
dataset._estimated_runtime_motion_frames = lambda record: (
120 if int(record["row_id"]) % 9 == 0 else 400
)
first = dataset._compose_sessions(records)
second = dataset._compose_sessions(records)
assert first == second
assert len(
{
session[0]["_session_interaction_mode"]
for session in first[: min(8, len(first))]
}
) == 2
by_mode: dict[str, list[dict]] = {
"turn-taking": [],
"interruption": [],
}
for session in first:
modes = {record["_session_interaction_mode"] for record in session}
voices = {record["_session_voice_name"] for record in session}
assert len(modes) == len(voices) == 1
mode = modes.pop()
by_mode[mode].extend(session)
assert 2 <= len(session) <= 4
if mode == "interruption":
assert all(
dataset._interruption_motion_is_eligible(record)
for record in session[:-1]
)
expected_rows = list(range(len(records)))
assert sorted(record["row_id"] for record in by_mode["turn-taking"]) == (
expected_rows
)
assert sorted(record["row_id"] for record in by_mode["interruption"]) == (
expected_rows
)
if split == "train":
row_voices = {
row_id: {
record["_session_voice_name"]
for mode_records in by_mode.values()
for record in mode_records
if int(record["row_id"]) == row_id
}
for row_id in expected_rows
}
assert all(len(voices) == 2 for voices in row_voices.values())
else:
row_voices = {
row_id: [
record["_session_voice_name"]
for mode_records in by_mode.values()
for record in mode_records
if int(record["row_id"]) == row_id
]
for row_id in expected_rows
}
assert all(len(set(voices)) == 1 for voices in row_voices.values())
def test_v21_independent_pools_preserve_complete_turn_taking_reference():
reference, reference_records = _session_voice_fixture("train")
reference_sessions = reference._compose_sessions(reference_records)
dataset, records = _session_voice_fixture("train")
dataset.interruption_sessions = True
dataset.interruption_ratio = 0.5
dataset.interruption_min_start_seconds = 2.0
dataset.interruption_max_start_seconds = 6.0
dataset.interruption_min_remaining_seconds = 1.5
dataset.motion_fps = 50
dataset.independent_task_pools = True
dataset.task_session_variants = {
"turn-taking": 2,
"interruption": 2,
"emergency-stop": 1,
"safe-hold": 1,
}
dataset._estimated_runtime_motion_frames = lambda record: 400
dataset._control_motion_is_eligible = lambda *args, **kwargs: True
sessions = dataset._compose_sessions(records)
by_mode = {
mode: [
session
for session in sessions
if session[0]["_session_interaction_mode"] == mode
]
for mode in dataset.task_session_variants
}
def signature(values):
return sorted(
(
int(session[0]["_session_voice_variant"]),
str(session[0]["_session_voice_name"]),
tuple(int(record["row_id"]) for record in session),
)
for session in values
)
assert signature(by_mode["turn-taking"]) == signature(reference_sessions)
assert sorted(
int(session[0]["_session_pool_index"])
for session in by_mode["turn-taking"]
) == list(range(len(reference_sessions)))
primitive_uses = {
mode: sum(len(session) for session in values)
for mode, values in by_mode.items()
}
assert primitive_uses == {
"turn-taking": 2 * len(records),
"interruption": 2 * len(records),
"emergency-stop": len(records),
"safe-hold": len(records),
}
assert dataset.control_task_rows_filtered == {
"emergency-stop": 0,
"safe-hold": 0,
}
def test_independent_pool_reuses_voice_only_after_full_coverage():
dataset, records = _session_voice_fixture("train")
dataset.interruption_sessions = True
dataset.interruption_ratio = 0.5
dataset.interruption_min_start_seconds = 2.0
dataset.interruption_max_start_seconds = 6.0
dataset.interruption_min_remaining_seconds = 1.5
dataset.motion_fps = 50
dataset.independent_task_pools = True
dataset.task_session_variants = {
"turn-taking": 5,
"interruption": 1,
"emergency-stop": 0,
"safe-hold": 0,
}
dataset._estimated_runtime_motion_frames = lambda record: 400
sessions = dataset._compose_sessions(records)
turn_rows = [
record
for session in sessions
if session[0]["_session_interaction_mode"] == "turn-taking"
for record in session
]
by_row = {
row_id: [
str(record["_session_voice_name"])
for record in turn_rows
if int(record["row_id"]) == row_id
]
for row_id in range(len(records))
}
assert all(len(voices) == 5 for voices in by_row.values())
assert all(len(set(voices)) == 4 for voices in by_row.values())
assert dataset.voice_reuse_after_coverage["turn-taking"] == len(records)
assert dataset.voice_reuse_after_coverage["interruption"] == 0
def test_balanced_control_pools_are_eight_percent_with_one_event_per_session():
dataset, records = _session_voice_fixture("train")
dataset.interruption_sessions = True
dataset.interruption_ratio = 0.5
dataset.interruption_min_start_seconds = 2.0
dataset.interruption_max_start_seconds = 6.0
dataset.interruption_min_remaining_seconds = 1.5
dataset.motion_fps = 50
dataset.independent_task_pools = True
dataset.task_session_variants = {
"turn-taking": 2,
"interruption": 2,
"emergency-stop": 1,
"safe-hold": 1,
}
dataset.control_task_session_ratio = 0.08
dataset.control_followup_ratio = 0.5
dataset._estimated_runtime_motion_frames = lambda record: 400
dataset._control_motion_is_eligible = lambda *args, **kwargs: True
unbalanced = dataset._compose_sessions(records)
sessions = dataset._balance_control_task_sessions(unbalanced)
counts = {
mode: sum(
session[0]["_session_interaction_mode"] == mode
for session in sessions
)
for mode in dataset.task_session_variants
}
assert counts["turn-taking"] + counts["interruption"] > 0
assert counts["emergency-stop"] == counts["safe-hold"]
assert counts["emergency-stop"] / len(sessions) == pytest.approx(
0.08,
abs=0.01,
)
control_sessions = [
session
for session in sessions
if session[0]["_session_interaction_mode"]
in {"emergency-stop", "safe-hold"}
]
for session in control_sessions:
turn_indices = {
int(record["_session_control_turn_index"])
for record in session
}
followup_flags = {
bool(record["_session_control_has_followup"])
for record in session
}
assert len(turn_indices) == len(followup_flags) == 1
control_turn = turn_indices.pop()
has_followup = followup_flags.pop()
assert has_followup == (control_turn + 1 < len(session))
assert any(
session[0]["_session_control_has_followup"]
for session in control_sessions
)
assert any(
not session[0]["_session_control_has_followup"]
for session in control_sessions
)
def test_v22_backchannel_final_session_mix_is_approximately_60_15_10_15():
dataset, records = _session_voice_fixture("train")
dataset.interruption_sessions = True
dataset.interruption_ratio = 0.5
dataset.interruption_min_start_seconds = 2.0
dataset.interruption_max_start_seconds = 6.0
dataset.interruption_min_remaining_seconds = 1.5
dataset.motion_fps = 50
dataset.independent_task_pools = True
dataset.task_session_variants = {
"turn-taking": 4,
"interruption": 1,
"emergency-stop": 2,
"safe-hold": 0,
"backchannel": 2,
}
dataset.interruption_task_session_ratio = 0.15
dataset.control_task_session_ratio = 0.10
dataset.backchannel_task_session_ratio = 0.15
dataset.control_followup_ratio = 0.5
dataset._estimated_runtime_motion_frames = lambda record: 400
dataset._control_motion_is_eligible = lambda *args, **kwargs: True
dataset._backchannel_motion_is_eligible = lambda *args, **kwargs: True
sessions = dataset._balance_backchannel_task_sessions(
dataset._compose_sessions(records)
)
counts = {
mode: sum(
session[0]["_session_interaction_mode"] == mode
for session in sessions
)
for mode in (
"turn-taking",
"interruption",
"emergency-stop",
"backchannel",
)
}
assert counts["turn-taking"] / len(sessions) == pytest.approx(
0.60,
abs=0.02,
)
assert counts["interruption"] / len(sessions) == pytest.approx(
0.15,
abs=0.02,
)
assert counts["emergency-stop"] / len(sessions) == pytest.approx(
0.10,
abs=0.02,
)
assert counts["backchannel"] / len(sessions) == pytest.approx(
0.15,
abs=0.02,
)
for session in sessions:
if session[0]["_session_interaction_mode"] != "backchannel":
continue
event_turns = {
int(record["_session_backchannel_turn_index"])
for record in session
}
assert len(event_turns) == 1
assert 0 <= event_turns.pop() < len(session)
def test_v23_appends_disfluency_windows_without_changing_v22_selection():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.split = "train"
dataset.split_seed = 42
dataset.task_session_variants = {
"turn-taking": 4,
"interruption": 1,
"emergency-stop": 1,
"safe-hold": 0,
"backchannel": 2,
"hesitation": 3,
"pause": 2,
}
dataset.interruption_task_session_ratio = 0.15
dataset.control_task_session_ratio = 0.10
dataset.backchannel_task_session_ratio = 0.15
dataset.hesitation_task_window_ratio = 0.0
dataset.pause_task_window_ratio = 0.0
dataset._cached_session_max_stream_steps = lambda session: int(
session[0]["estimated_steps"]
)
def sessions(mode: str, count: int, *, offset: int):
return tuple(
(
{
"row_id": offset + index,
"_session_interaction_mode": mode,
"estimated_steps": 10,
},
)
for index in range(count)
)
legacy_pool = (
*sessions("turn-taking", 100, offset=0),
*sessions("interruption", 80, offset=1_000),
*sessions("emergency-stop", 80, offset=2_000),
*sessions("backchannel", 80, offset=3_000),
)
v22 = dataset._balance_backchannel_task_sessions(legacy_pool)
v22_ids = {
int(session[0]["row_id"])
for session in v22
}
dataset.hesitation_task_window_ratio = 0.12
dataset.pause_task_window_ratio = 0.08
v23 = dataset._balance_backchannel_task_sessions(
(
*legacy_pool,
*sessions("hesitation", 80, offset=4_000),
*sessions("pause", 80, offset=5_000),
)
)
legacy_ids_after = {
int(session[0]["row_id"])
for session in v23
if session[0]["_session_interaction_mode"]
not in {"hesitation", "pause"}
}
by_mode_windows = {
mode: sum(
int(session[0]["estimated_steps"])
for session in v23
if session[0]["_session_interaction_mode"] == mode
)
for mode in ("hesitation", "pause")
}
total_windows = sum(
int(session[0]["estimated_steps"])
for session in v23
)
assert legacy_ids_after == v22_ids
assert by_mode_windows["hesitation"] / total_windows == pytest.approx(
0.12,
abs=0.01,
)
assert by_mode_windows["pause"] / total_windows == pytest.approx(
0.08,
abs=0.01,
)
def test_new_disfluency_rows_are_duration_stratified_and_exact_prompt_capped():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.split = "train"
dataset.session_min_turns = 2
dataset.new_task_max_rows_per_instruction_train = 3
dataset.new_task_max_rows_per_instruction_val = 2
dataset.disfluency_instruction_segments = {
("seed_audio/shared", "female_01_en"): {"prefix": object(), "suffix": object()}
}
dataset._session_voice_names = lambda record: ("female_01_en",)
records = [
{
"row_id": index,
"audio_dir": "seed_audio/shared",
"instruction": "walk forward",
"instruction_zh": "向前走",
"actual_frames": frames,
}
for index, frames in enumerate((10, 20, 30, 40, 50))
]
selected = dataset._select_new_task_records(records)
assert [record["actual_frames"] for record in selected] == [10, 30, 50]
assert dataset.new_task_record_selection_summary == {
"input_rows": 5,
"eligible_rows": 5,
"selected_rows": 3,
"instruction_pairs": 1,
"cap_per_instruction": 3,
"unsupported_rows": 0,
}
def _write_test_pcm16(path: Path, values: np.ndarray, rate: int = 16_000) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
pcm = np.clip(np.asarray(values), -0.999, 0.999)
pcm = np.rint(pcm * 32767.0).astype("<i2")
with wave.open(str(path), "wb") as handle:
handle.setnchannels(1)
handle.setsampwidth(2)
handle.setframerate(rate)
handle.writeframes(pcm.tobytes())
def test_disfluency_speech_keeps_pause_and_repair_inside_one_causal_utterance(
tmp_path,
):
rate = 16_000
stride = 6_400
voice_name = "female_01_en"
prefix_path = tmp_path / "final-prefix.wav"
suffix_path = tmp_path / "final-suffix.wav"
provisional_path = tmp_path / "provisional-prefix.wav"
filler_path = tmp_path / "filler.wav"
repair_path = tmp_path / "repair.wav"
final_path = tmp_path / "final-complete.wav"
_write_test_pcm16(prefix_path, np.full(1_000, 0.10, np.float32))
_write_test_pcm16(suffix_path, np.full(1_200, 0.20, np.float32))
_write_test_pcm16(provisional_path, np.full(900, -0.10, np.float32))
_write_test_pcm16(filler_path, np.full(800, 0.30, np.float32))
_write_test_pcm16(repair_path, np.full(700, -0.30, np.float32))
_write_test_pcm16(final_path, np.full(1_500, 0.15, np.float32))
def segment(
category: str,
segment_id: str,
path: Path,
text: str,
*,
role: str = "",
) -> DisfluencyAudioSource:
with wave.open(str(path), "rb") as handle:
count = handle.getnframes()
return DisfluencyAudioSource(
category=category,
segment_id=segment_id,
split="train" if category != "instruction_segment" else "shared",
voice_name=voice_name,
language="en",
text=text,
waveform_path=path,
speech_samples=count,
segment_role=role,
)
final_roles = {
"prefix": segment(
"instruction_segment", "final-prefix", prefix_path, "walk", role="prefix"
),
"suffix": segment(
"instruction_segment", "final-suffix", suffix_path, "forward", role="suffix"
),
}
provisional_roles = {
"prefix": segment(
"instruction_segment",
"provisional-prefix",
provisional_path,
"pick up",
role="prefix",
),
"suffix": segment(
"instruction_segment",
"unused-provisional-suffix",
suffix_path,
"the box",
role="suffix",
),
}
filler = segment("hesitation_filler", "fill-1", filler_path, "um")
repair = segment("repair_cue", "repair-1", repair_path, "I mean")
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.split = "train"
dataset.audio_sampling_rate = rate
dataset.motion_fps = 50
dataset.audio_sample_step_frames = 20
dataset.audio_window_seconds = 0.6
assert dataset.audio_stride_samples == stride
assert dataset.audio_window_samples == 9_600
dataset.disfluency_instruction_segments = {
("seed_audio/final", voice_name): final_roles,
("seed_audio/provisional", voice_name): provisional_roles,
}
dataset.disfluency_marker_catalog = {
voice_name: {
"hesitation_filler": (filler,),
"repair_cue": (repair,),
}
}
voice = AudioSource(
source_key=f"seed_audio/final/{voice_name}",
clips=(),
waveform_path=final_path,
waveform_count=1,
)
assignment = SourceBackgroundAssignment(
source_key=voice.source_key,
background_key="office",
listen_start_index=3,
listen_window_count=1,
background_window_count=100,
initial_idle_max_steps=7,
max_motion_steps=0,
safety_steps=2,
mixed_audio_path=tmp_path / "unused.wav",
original_audio_path=final_path,
speech_samples=1_500,
speech_scale=1.0,
)
record = {
"audio_dir": "seed_audio/final",
"instruction": "walk forward",
"instruction_zh": "向前走",
}
_, pause_assignment, pause_speech, pause_metadata = (
dataset._build_disfluency_turn_speech(
record=record,
voice=voice,
assignment=assignment,
subtype="silent-pause",
gap_steps=2,
)
)
pause_roles = [row["role"] for row in pause_metadata["components"]]
assert pause_roles == [
"final-prefix",
"background-only-pause",
"final-suffix",
]
assert pause_speech.size == 1_000 + 2 * stride + 1_200
np.testing.assert_allclose(pause_speech[1_000 : 1_000 + 2 * stride], 0.0)
assert pause_assignment.speech_samples == pause_speech.size
assert pause_assignment.listen_window_count == (
dataset._runtime_listen_window_count(pause_speech.size)
)
_, _, repair_speech, repair_metadata = dataset._build_disfluency_turn_speech(
record=record,
voice=voice,
assignment=assignment,
subtype="self-repair",
gap_steps=2,
marker_id="repair-1",
provisional_audio_dir="seed_audio/provisional",
provisional_instruction="pick up the box",
provisional_instruction_zh="拿起箱子",
)
repair_roles = [row["role"] for row in repair_metadata["components"]]
assert repair_roles == [
"provisional-prefix",
"repair-background-before",
"repair-cue",
"repair-background-after",
"complete-final-instruction",
]
assert repair_speech.size == 900 + 2 * stride + 1_500
assert repair_metadata["provisional_instruction"] == "pick up the box"
assert repair_metadata["final_instruction"] == "walk forward"
_, _, implicit_speech, implicit_metadata = (
dataset._build_disfluency_turn_speech(
record=record,
voice=voice,
assignment=assignment,
subtype="self-repair",
repair_style="implicit-restart",
gap_steps=2,
provisional_audio_dir="seed_audio/provisional",
provisional_instruction="pick up the box",
provisional_instruction_zh="拿起箱子",
)
)
implicit_roles = [
row["role"] for row in implicit_metadata["components"]
]
assert implicit_roles == [
"provisional-prefix",
"repair-implicit-background",
"complete-final-instruction",
]
assert implicit_speech.size == 900 + 2 * stride + 1_500
assert implicit_metadata["marker_id"] is None
assert implicit_metadata["marker_text"] == ""
assert implicit_metadata["repair_style"] == "implicit-restart"
assert "repair-implicit-background" in DISFLUENCY_UNCERTAINTY_ROLES
def test_hesitation_pause_splitter_never_cuts_inside_domain_words():
module_path = (
Path(__file__).resolve().parents[1]
/ "scripts/data/generate_hfd_hesitation_pause.py"
)
spec = importlib.util.spec_from_file_location(
"hfd_test_hesitation_pause_generator",
module_path,
)
assert spec is not None and spec.loader is not None
generator = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = generator
spec.loader.exec_module(generator)
pytest.importorskip("jieba")
for text, protected in (
("双臂交叉站立", "交叉"),
("拄拐顺时针缓慢走路", "顺时针"),
("向后退五步", "后退"),
):
split = generator.split_chinese(text)
assert split is not None
prefix, suffix = split
assert prefix + suffix == text
boundary = len(prefix)
protected_start = text.index(protected)
assert not protected_start < boundary < protected_start + len(protected)
def test_hesitation_pause_existing_index_is_relative_and_safe(tmp_path):
module_path = (
Path(__file__).resolve().parents[1]
/ "scripts/data/generate_hfd_hesitation_pause.py"
)
spec = importlib.util.spec_from_file_location(
"hfd_test_hesitation_pause_resume_index",
module_path,
)
assert spec is not None and spec.loader is not None
generator = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = generator
spec.loader.exec_module(generator)
index = tmp_path / "existing.txt"
index.write_text(
"audio/instruction_segments/000006/female_01_en_prefix.wav\n"
"audio/markers/train/hes_fill_en_01/female_01_en.wav\n",
encoding="utf-8",
)
assert generator.load_existing_output_index(index, tmp_path) == {
"audio/instruction_segments/000006/female_01_en_prefix.wav",
"audio/markers/train/hes_fill_en_01/female_01_en.wav",
}
index.write_text("../outside.wav\n", encoding="utf-8")
with pytest.raises(ValueError, match="Unsafe existing-output index"):
generator.load_existing_output_index(index, tmp_path)
def test_hesitation_pause_manifest_audit_rejects_runaway_tts(tmp_path):
module_path = (
Path(__file__).resolve().parents[1]
/ "scripts/data/generate_hfd_hesitation_pause.py"
)
spec = importlib.util.spec_from_file_location(
"hfd_test_hesitation_pause_manifest_audit",
module_path,
)
assert spec is not None and spec.loader is not None
generator = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = generator
spec.loader.exec_module(generator)
valid = tmp_path / "valid.wav"
with wave.open(str(valid), "wb") as handle:
handle.setnchannels(1)
handle.setsampwidth(2)
handle.setframerate(16_000)
handle.writeframes(np.full(16_000, 1_024, dtype="<i2").tobytes())
fields = generator._manifest_audio_fields(valid)
assert fields["speech_samples_16k"] == 16_000
assert fields["duration_seconds"] == pytest.approx(1.0)
assert fields["sha256"] == hashlib.sha256(valid.read_bytes()).hexdigest()
runaway = tmp_path / "runaway.wav"
with wave.open(str(runaway), "wb") as handle:
handle.setnchannels(1)
handle.setsampwidth(2)
handle.setframerate(16_000)
handle.writeframes(np.zeros(20 * 16_000, dtype="<i2").tobytes())
with pytest.raises(RuntimeError, match="invalid PCM16/16kHz/mono"):
generator._manifest_audio_fields(runaway)
def test_v24_hesitation_inventory_has_long_fillers_and_implicit_repair():
module_path = (
Path(__file__).resolve().parents[1]
/ "scripts/data/generate_hfd_hesitation_pause.py"
)
spec = importlib.util.spec_from_file_location(
"hfd_test_hesitation_pause_v24_inventory",
module_path,
)
assert spec is not None and spec.loader is not None
generator = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = generator
spec.loader.exec_module(generator)
vocalized = [
row
for row in generator.MARKERS
if row.get("delivery") == "vocalized_filler"
]
phrases = [
row
for row in generator.MARKERS
if row.get("delivery") == "phrase_filler"
]
assert vocalized
assert phrases
assert all(float(row["min_duration_seconds"]) >= 0.8 for row in vocalized)
assert all(float(row["min_duration_seconds"]) >= 0.4 for row in phrases)
repair_styles = {
str(row.get("repair_style", "explicit-cue"))
for row in generator.REVIEW_EXAMPLES
if row["subtype"] == "self-repair"
}
assert repair_styles == {"explicit-cue", "implicit-restart"}
def test_longest_point_one_percent_is_dropped_inside_each_task_mode():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.long_session_drop_ratio = 0.001
sessions = tuple(
(
{
"row_id": mode_index * 2_000 + index,
"_session_interaction_mode": mode,
"estimated_steps": index + 1,
},
)
for mode_index, mode in enumerate(
("turn-taking", "interruption", "emergency-stop")
)
for index in range(1_000)
)
dataset.sessions = sessions
dataset._cached_session_max_stream_steps = lambda session: int(
session[0]["estimated_steps"]
)
dataset.long_session_filter_summary = {}
retained = dataset._drop_longest_sessions_by_mode(sessions)
assert len(retained) == len(sessions) - 3
for mode in ("turn-taking", "interruption", "emergency-stop"):
values = [
session[0]["estimated_steps"]
for session in retained
if session[0]["_session_interaction_mode"] == mode
]
assert len(values) == 999
assert max(values) == 999
assert dataset.long_session_filter_summary[mode]["dropped"] == 1
def test_control_command_only_uses_motion_with_complete_causal_tail():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.action_exec_horizon = 20
dataset.motion_fps = 50
dataset.turn_transition_frames = 50
dataset.interruption_min_start_seconds = 2.0
dataset.interruption_max_start_seconds = 6.0
dataset.control_command_catalog = {
"emergency-stop": {
"female_01_zh": (
SimpleNamespace(speech_samples=16_000),
)
}
}
dataset._runtime_listen_window_count = lambda samples: 3
dataset._estimated_runtime_motion_frames = lambda record: int(
record["frames"]
)
# 100-frame onset + 60-frame complete command + 20-frame real tail.
assert not dataset._control_motion_is_eligible(
{"frames": 130},
intent="emergency-stop",
voice_name="female_01_zh",
)
assert dataset._control_motion_is_eligible(
{"frames": 131},
intent="emergency-stop",
voice_name="female_01_zh",
)
start, diagnostics = dataset._sample_control_speech_start(
np.zeros((180, 36), dtype=np.float32),
reaction_frames=60,
)
assert start == 100
assert diagnostics["minimum_remaining_frames"] == 20
def test_control_eligibility_respects_debug_motion_truncation():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.action_exec_horizon = 20
dataset.prefix_frames = 10
dataset.motion_fps = 50
dataset.turn_transition_frames = 50
dataset.dynamic_task_entry_blend = True
dataset.control_min_start_seconds = 1.2
dataset.control_min_remaining_seconds = 2.0
dataset.control_command_catalog = {
"emergency-stop": {
"female_01_zh": (SimpleNamespace(speech_samples=16_000),)
}
}
dataset._runtime_listen_window_count = lambda samples: 3
dataset._estimated_runtime_motion_frames = lambda record: 1_000
# Runtime truncation to 200 frames cannot contain a 60-frame onset,
# 60-frame utterance and 100-frame post-command motion tail.
dataset.max_motion_steps = 10
assert not dataset._control_motion_is_eligible(
{},
intent="emergency-stop",
voice_name="female_01_zh",
)
dataset.max_motion_steps = 12
assert dataset._control_motion_is_eligible(
{},
intent="emergency-stop",
voice_name="female_01_zh",
)
def test_activity_aware_stop_prefers_motion_over_stationary_tail():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.action_exec_horizon = 20
dataset.motion_fps = 50
dataset.turn_transition_frames = 50
dataset.interruption_min_start_seconds = 2.0
dataset.interruption_max_start_seconds = 6.0
dataset.control_min_start_seconds = 1.2
dataset.control_max_start_seconds = 4.0
dataset.control_min_remaining_seconds = 2.0
dataset.control_activity_aware_timing = True
sequence = np.zeros((400, 36), dtype=np.float32)
sequence[:, 32] = 1.0
# Real task motion is active from 1.0 s through 3.6 s, followed by a long
# stationary tail. A 1.2 s command must be placed so both its onset and
# its complete-response boundary still observe movement.
sequence[50:181, 0] = np.arange(131, dtype=np.float32) * 0.02
sequence[181:, 0] = sequence[180, 0]
random.seed(7)
start, diagnostics = dataset._sample_control_speech_start(
sequence,
reaction_frames=60,
)
assert 60 <= start <= 120
assert start + 60 + 100 <= len(sequence)
assert diagnostics["active_candidate_count"] > 0
assert diagnostics["activity_fallback"] is False
def test_safe_hold_waits_for_supported_low_velocity_pose():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.action_exec_horizon = 20
dataset.safe_hold_stability_window_frames = 20
sequence = np.zeros((260, 36), dtype=np.float32)
sequence[:, 32] = 1.0
# The robot keeps moving through frame 159, then reaches and retains an
# upright, supported pose. A safe hold must not freeze during that motion.
sequence[:160, 0] = np.arange(160, dtype=np.float32) * 0.20
sequence[160:, 0] = sequence[159, 0]
boundary = dataset._safe_hold_boundary(sequence, earliest_frame=100)
assert boundary >= 180
assert boundary < len(sequence)
def test_emergency_recovery_prefers_nearby_two_support_phase():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.safe_stationary_anchors = True
dataset.action_exec_horizon = 20
dataset.turn_transition_frames = 50
dataset._stationary_joint_scale = np.ones(29, dtype=np.float32)
neutral = np.zeros(36, dtype=np.float32)
neutral[31] = 0.82
neutral[32] = 1.0
dataset._stationary_anchor_poses = neutral[None, :]
sequence = np.repeat(neutral[None, :], 100, axis=0)
# At the immediate stop boundary both leg chains are moving; shortly
# afterwards they settle close to a two-support neutral pose. Recovery
# searches every frame so it cannot miss a brief safe-support phase.
sequence[:25, 0:12] = np.linspace(
0.0, 1.0, 25, dtype=np.float32
)[:, None]
sequence[25:40, 0:12] = np.linspace(
1.0, 0.0, 15, dtype=np.float32
)[:, None]
boundary, anchor = dataset._nearest_safe_recovery_boundary(
sequence,
earliest_frame=20,
)
assert 39 <= boundary <= 41
np.testing.assert_allclose(anchor[:29], neutral[:29], atol=1e-6)
def test_state_aware_serial_control_consumes_only_causal_physical_context():
torch.manual_seed(7)
model = FullDuplexPsi0Model.__new__(FullDuplexPsi0Model)
nn.Module.__init__(model)
model.state_aware_serial_control = True
model.action_dim = 3
memory_dim = 3
model.control_state_encoder = nn.Sequential(
nn.LayerNorm(3), nn.Linear(3, memory_dim), nn.GELU()
)
model.control_rtc_encoder = nn.Sequential(
nn.LayerNorm(3), nn.Linear(3, memory_dim), nn.GELU()
)
model.control_progress_encoder = nn.Sequential(
nn.Linear(5, memory_dim), nn.GELU()
)
model.previous_control_embedding = nn.Embedding(4, memory_dim)
model.control_head = nn.Sequential(
nn.LayerNorm(4 + 4 * memory_dim + 1),
nn.Linear(4 + 4 * memory_dim + 1, memory_dim),
nn.GELU(),
nn.Linear(memory_dim, 3),
)
semantic = torch.randn(1, 4, requires_grad=True)
state = torch.tensor([[[0.1, -0.2, 0.3]]], requires_grad=True)
progress = torch.tensor(
[[0.25, 0.75, 0.25, 1.0, 0.0]], requires_grad=True
)
rtc = torch.randn(1, 10, 3, requires_grad=True)
rtc_mask = torch.ones(1, 10, dtype=torch.bool)
logits = model._predict_control_logits(
semantic,
states=state,
control_progress=progress,
previous_control_ids=torch.tensor([MOTION_TOKEN_ID]),
control_rtc_prefix=rtc,
control_rtc_mask=rtc_mask,
)
logits.square().sum().backward()
assert logits.shape == (1, 3)
for value in (semantic.grad, state.grad, progress.grad, rtc.grad):
assert value is not None
assert torch.isfinite(value).all()
assert float(value.abs().sum()) > 0.0
# A masked source-start RTC contains no causal plan, so its numeric filler
# cannot change the decision.
source_start_a = model._predict_control_logits(
semantic.detach(),
states=state.detach(),
control_progress=progress.detach(),
previous_control_ids=torch.tensor([3]),
control_rtc_prefix=torch.zeros_like(rtc),
control_rtc_mask=torch.zeros_like(rtc_mask),
)
source_start_b = model._predict_control_logits(
semantic.detach(),
states=state.detach(),
control_progress=progress.detach(),
previous_control_ids=torch.tensor([3]),
control_rtc_prefix=torch.full_like(rtc, 999.0),
control_rtc_mask=torch.zeros_like(rtc_mask),
)
torch.testing.assert_close(source_start_a, source_start_b)
with pytest.raises(ValueError, match="missing causal inputs"):
model._predict_control_logits(
semantic.detach(),
states=state.detach(),
control_progress=None,
previous_control_ids=torch.tensor([3]),
control_rtc_prefix=torch.zeros_like(rtc),
control_rtc_mask=torch.zeros_like(rtc_mask),
)
def test_semantic_only_serial_control_ignores_physical_context():
torch.manual_seed(11)
model = FullDuplexPsi0Model.__new__(FullDuplexPsi0Model)
nn.Module.__init__(model)
model.state_aware_serial_control = False
model.control_head = nn.Sequential(
nn.LayerNorm(4),
nn.Linear(4, 3),
)
semantic = torch.randn(2, 4, requires_grad=True)
logits_a = model._predict_control_logits(
semantic,
states=torch.randn(2, 1, 39),
control_progress=torch.randn(2, 5),
previous_control_ids=torch.tensor([0, 2]),
control_rtc_prefix=torch.randn(2, 10, 39),
control_rtc_mask=torch.ones(2, 10, dtype=torch.bool),
)
logits_b = model._predict_control_logits(
semantic,
states=None,
control_progress=None,
previous_control_ids=None,
control_rtc_prefix=None,
control_rtc_mask=None,
)
torch.testing.assert_close(logits_a, logits_b)
logits_b.sum().backward()
assert semantic.grad is not None
assert float(semantic.grad.abs().sum()) > 0.0
def test_interruption_source_continues_old_motion_then_transitions_causally(
monkeypatch,
):
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
records = tuple(
{
"row_id": row_id,
"filename": f"motion-{row_id}",
"instruction": f"instruction-{row_id}",
"audio_dir": f"audio-{row_id}",
"motion_dir": Path(f"/unused/motion-{row_id}"),
"declared_frames": 350,
"_session_interaction_mode": "interruption",
}
for row_id in (3, 7)
)
dataset.records = list(records)
dataset.sessions = (records,)
dataset.multi_turn_sessions = True
dataset.interruption_sessions = True
dataset.interruption_ratio = 0.5
dataset.interruption_min_start_seconds = 2.0
dataset.interruption_max_start_seconds = 6.0
dataset.interruption_min_remaining_seconds = 1.5
dataset.session_min_turns = 2
dataset.session_max_turns = 4
dataset.fixed_voice_per_record = True
dataset.action_chunk_size = 30
dataset.action_exec_horizon = 20
dataset.prefix_frames = 10
dataset.audio_sample_step_frames = 20
dataset.action_sample_step_frames = 1
dataset.audio_sampling_rate = 16_000
dataset.motion_fps = 50
dataset.audio_window_seconds = 0.6
dataset.initial_idle_max_steps = 7
dataset.stationary_blend_frames = 20
dataset.turn_transition_frames = 50
dataset.causal_listen_timing = True
dataset.max_speech_onset_steps = 3
dataset.listen_reaction_steps = 1
dataset.max_motion_steps = 0
dataset.max_stream_steps = 1923
voices = tuple(
AudioSource(
source_key=f"audio-{row_id}/voice",
clips=(),
waveform_path=Path(f"/unused/voice-{row_id}.wav"),
waveform_count=7,
)
for row_id in (3, 7)
)
dataset.audio_catalog = {
"audio-3": (voices[0],),
"audio-7": (voices[1],),
}
dataset.background_assignments = {
voice.source_key: SourceBackgroundAssignment(
source_key=voice.source_key,
background_key="office",
listen_start_index=20,
listen_window_count=7,
background_window_count=400,
initial_idle_max_steps=7,
max_motion_steps=30,
safety_steps=5,
original_audio_path=Path(f"/unused/{voice.source_key}.wav"),
speech_samples=39_680,
speech_scale=0.5,
)
for voice in voices
}
motions = {}
for record_index, record in enumerate(records):
motion = np.zeros((350, 36), dtype=np.float32)
motion[:, 0] = np.linspace(0.0, 0.4 + record_index, 350)
motion[:, 29] = np.linspace(0.0, 4.0 + record_index, 350)
motion[:, 31] = 0.8
motion[:, 32] = 1.0
motions[str(record["motion_dir"])] = motion
monkeypatch.setattr(dataset, "_load_motion", lambda path: motions[str(path)])
monkeypatch.setattr(
dataset,
"_sample_stationary_anchor",
lambda pose: np.asarray(pose, dtype=np.float32).copy(),
)
monkeypatch.setattr(
dataset,
"_sample_stationary_sequence",
lambda phase, frame_count, anchor_pose, **kwargs: (
np.repeat(
np.asarray(anchor_pose, dtype=np.float32)[None, :],
frame_count,
axis=0,
),
f"{phase}-fixture",
),
)
clean_speech = np.zeros(39_680, dtype=np.float32)
clean_speech[17_120:] = 0.1
monkeypatch.setattr(
"psi.data.seed.full_duplex_audio_command_dataset._read_resampled_speech_float",
lambda path, expected_rate: clean_speech.copy(),
)
monkeypatch.setattr(
dataset,
"_build_session_audio_windows",
lambda *, turn_audio, semantic_window_count: (
tuple(
np.full(9600, float(index), dtype=np.float32)
for index in range(semantic_window_count)
),
"office",
100,
),
)
monkeypatch.setattr(
dataset,
"_sample_interruption_prefix_frames",
lambda task_sequence, next_reaction_frames: 300,
)
random.seed(20)
source = dataset[0]
assert source["interaction_mode"] == "interruption"
assert len(source["interruption_start_frames"]) == 1
speech_start = source["turn_speech_start_frames"][1]
listen_start = source["turn_listen_start_frames"][1]
assert speech_start == source["interruption_start_frames"][0]
first_motion_start = source["turn_listen_start_frames"][0] + 7 * 20
assert 100 <= speech_start - first_motion_start <= 300
assert listen_start - speech_start == 100
assert all(
source["steps"][frame]["control_label"] == MOTION_TOKEN_ID
for frame in range(speech_start, listen_start)
)
assert source["steps"][listen_start]["control_label"] == LISTEN_TOKEN_ID
assert source["steps"][listen_start]["control_transition"] is True
np.testing.assert_allclose(
source["steps"][listen_start - 1]["actions"][0],
source["steps"][listen_start]["states"][0],
atol=1e-6,
)
original_before = source["interruption_original_frames"][0]
assert source["interruption_remaining_frames"][0] >= 75
assert source["interruption_terminal_hold_frames"] == [1]
expected_progress = (original_before + 2) / 349.0
assert source["steps"][speech_start]["future_progress"][0, 0] == pytest.approx(
expected_progress
)
assert len(source["recovery_transition_frames"]) == 1
assert all(
step["interaction_mode"] == "interruption"
for step in source["steps"]
)
def test_multiturn_source_keeps_one_cache_id_and_aligned_boundaries(monkeypatch):
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
records = tuple(
{
"row_id": row_id,
"filename": f"motion-{row_id}",
"instruction": f"instruction-{row_id}",
"audio_dir": f"audio-{row_id}",
"motion_dir": Path(f"/unused/motion-{row_id}"),
"declared_frames": 96,
}
for row_id in (3, 7)
)
dataset.records = list(records)
dataset.sessions = (records,)
dataset.multi_turn_sessions = True
dataset.session_min_turns = 2
dataset.session_max_turns = 4
dataset.fixed_voice_per_record = True
dataset.action_chunk_size = 30
dataset.action_exec_horizon = 20
dataset.prefix_frames = 10
dataset.audio_sample_step_frames = 20
dataset.action_sample_step_frames = 1
dataset.audio_sampling_rate = 16_000
dataset.motion_fps = 50
dataset.audio_window_seconds = 0.6
dataset.initial_idle_max_steps = 7
dataset.stationary_blend_frames = 20
dataset.turn_transition_frames = 50
dataset.causal_listen_timing = True
dataset.max_speech_onset_steps = 3
dataset.listen_reaction_steps = 1
dataset.max_motion_steps = 0
dataset.max_stream_steps = 1923
voices = tuple(
AudioSource(
source_key=f"audio-{row_id}/voice",
clips=(),
waveform_path=Path(f"/unused/voice-{row_id}.wav"),
waveform_count=7,
)
for row_id in (3, 7)
)
dataset.audio_catalog = {
"audio-3": (voices[0],),
"audio-7": (voices[1],),
}
dataset.background_assignments = {
voice.source_key: SourceBackgroundAssignment(
source_key=voice.source_key,
background_key="office",
listen_start_index=20,
listen_window_count=7,
background_window_count=400,
initial_idle_max_steps=5,
max_motion_steps=20,
safety_steps=5,
original_audio_path=Path(f"/unused/voice-{voice.source_key}.wav"),
speech_samples=39_680,
speech_scale=0.5,
)
for voice in voices
}
motions = {}
for record_index, record in enumerate(records):
motion = np.zeros((40, 36), dtype=np.float32)
motion[:, 0] = np.linspace(0.0, 0.2 + record_index, 40)
motion[:, 29] = np.linspace(0.0, 0.5, 40)
motion[:, 31] = 0.8
motion[:, 32] = 1.0
motions[str(record["motion_dir"])] = motion
monkeypatch.setattr(
dataset,
"_load_motion",
lambda path: motions[str(path)],
)
monkeypatch.setattr(
dataset,
"_sample_stationary_anchor",
lambda pose: np.asarray(pose, dtype=np.float32).copy(),
)
monkeypatch.setattr(
dataset,
"_sample_stationary_sequence",
lambda phase, frame_count, anchor_pose, **kwargs: (
np.repeat(
np.asarray(anchor_pose, dtype=np.float32)[None, :],
frame_count,
axis=0,
),
f"{phase}-fixture",
),
)
# 1.07 s of leading silence needs four Qwen boundaries, deliberately
# exceeding the historical expected budget of three. It is valid speech
# and must extend the idle reaction phase instead of killing a worker.
clean_speech = np.zeros(39_680, dtype=np.float32)
clean_speech[17_120:] = 0.1
monkeypatch.setattr(
"psi.data.seed.full_duplex_audio_command_dataset._read_resampled_speech_float",
lambda path, expected_rate: clean_speech.copy(),
)
def build_audio(*, turn_audio, semantic_window_count):
assert len(turn_audio) == 2
windows = tuple(
np.full(9600, float(index), dtype=np.float32)
for index in range(semantic_window_count)
)
return windows, "office", 100
monkeypatch.setattr(dataset, "_build_session_audio_windows", build_audio)
random.seed(19)
source = dataset[0]
assert source["turn_count"] == 2
assert source["task_transition_frames"] == [50, 50]
assert all(50 <= count <= 69 for count in source["recovery_transition_frames"])
assert all(frame % 20 == 0 for frame in source["turn_listen_start_frames"])
assert all(
listen - speech == 100
for speech, listen in zip(
source["turn_speech_start_frames"],
source["turn_listen_start_frames"],
)
)
assert source["turn_listen_reaction_frames"] == [100, 100]
assert source["listen_transition_frames"] == 50
assert {step["source_id"] for step in source["steps"]} == {
source["source_id"]
}
update_steps = [
step for step in source["steps"] if step["update_semantic_cache"]
]
assert [step["background_window_index"] for step in update_steps] == list(
range(100, 100 + len(update_steps))
)
transitions = [
step
for step in update_steps[1:]
if step["control_label"]
!= update_steps[step["semantic_window_index"] - 1]["control_label"]
]
assert transitions
assert all(step["control_transition"] for step in transitions)
assert {step["turn_index"] for step in source["steps"]} == {0, 1}
duration_targets = [
step
for step in source["steps"]
if step["motion_duration_target_mask"]
]
assert len(duration_targets) == source["turn_count"] == 2
assert all(step["update_semantic_cache"] for step in duration_targets)
assert all(step["control_label"] == MOTION_TOKEN_ID for step in duration_targets)
assert [step["motion_duration_target_seconds"] for step in duration_targets] == [
0.8,
0.8,
]
text_targets = [
step
for step in source["steps"]
if step["latest_instruction_target_mask"]
]
assert [step["latest_instruction_text"] for step in text_targets] == [
"instruction-3",
"instruction-7",
]
assert all(step["update_semantic_cache"] for step in text_targets)
memory_targets = [
step
for step in source["steps"]
if step["instruction_memory_target_mask"]
]
assert len(memory_targets) == 1
assert memory_targets[0]["turn_index"] == 1
assert memory_targets[0]["instruction_memory_slot"] == "previous"
assert memory_targets[0]["instruction_memory_text"] == "instruction-3"
assert memory_targets[0]["update_semantic_cache"] is True
def test_multiturn_audio_uses_one_pcm_continuum(monkeypatch):
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.audio_sampling_rate = 16_000
dataset.audio_window_seconds = 0.6
dataset.audio_sample_step_frames = 20
dataset.motion_fps = 50
dataset.background_catalog = {
"office": BackgroundWaveformSource(
source_key="office",
waveform_path=Path("/unused/office.wav"),
window_count=20,
)
}
flat_background = np.linspace(-0.02, 0.02, 60_000, dtype=np.float32)
monkeypatch.setattr(
"psi.data.seed.full_duplex_audio_command_dataset._read_pcm16_wave_segment",
lambda path, start_sample, sample_count, expected_rate: (
flat_background[:sample_count].copy()
),
)
speech = {
"/unused/one.wav": np.full(3_200, 0.1, dtype=np.float32),
"/unused/two.wav": np.full(4_800, -0.1, dtype=np.float32),
}
monkeypatch.setattr(
"psi.data.seed.full_duplex_audio_command_dataset._read_resampled_speech_float",
lambda path, expected_rate: speech[str(path)].copy(),
)
monkeypatch.setattr("random.randrange", lambda stop: 0)
turn_audio = []
for source_key, path, samples, frame in (
("one", "/unused/one.wav", 3_200, 20),
("two", "/unused/two.wav", 4_800, 100),
):
turn_audio.append(
{
"listen_start_frame": frame,
"assignment": SourceBackgroundAssignment(
source_key=source_key,
background_key="old-single-turn-background",
listen_start_index=5,
listen_window_count=2,
background_window_count=20,
initial_idle_max_steps=5,
max_motion_steps=5,
safety_steps=2,
original_audio_path=Path(path),
speech_samples=samples,
speech_scale=0.5,
),
}
)
windows, key, start = dataset._build_session_audio_windows(
turn_audio=turn_audio,
semantic_window_count=8,
)
assert key == "office"
assert start == 0
assert len(windows) == 8
overlap = dataset.audio_window_samples - dataset.audio_stride_samples
for left, right in zip(windows, windows[1:]):
np.testing.assert_array_equal(left[-overlap:], right[:overlap])
assert not np.array_equal(windows[1], flat_background[6_400:16_000])
def test_turn_transition_is_slow_smooth_and_endpoint_exact():
start = np.zeros(36, dtype=np.float32)
end = np.zeros(36, dtype=np.float32)
start[31] = 0.80
end[:29] = 0.5
end[29:32] = np.asarray([1.0, -0.5, 0.86], dtype=np.float32)
start[32] = 1.0
end[32] = 1.0
transition = FullDuplexAudioCommandDataset._build_pose_transition(
start,
end,
50,
)
np.testing.assert_allclose(transition[0], start, atol=1e-7)
np.testing.assert_allclose(transition[-1], end, atol=1e-7)
# Quintic smootherstep approaches both endpoints much more gently than a
# direct 20-frame interpolation; its first increment is below 0.01% of
# the complete joint displacement.
assert float(np.max(np.abs(transition[1, :29] - start[:29]))) < 5e-5
assert float(np.max(np.abs(end[:29] - transition[-2, :29]))) < 5e-5
np.testing.assert_allclose(
np.linalg.norm(transition[:, 32:36], axis=-1),
1.0,
atol=1e-6,
)
def test_multiturn_task_entry_uses_relaxed_quintic_transition():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.multi_turn_sessions = True
dataset.stationary_blend_frames = 20
anchor = np.zeros(36, dtype=np.float32)
anchor[31] = 0.82
anchor[32] = 1.0
task = np.repeat(anchor[None, :], 80, axis=0)
task[:, 0] = 0.6
sequence, transition_frames = dataset._build_task_sequence(
task,
stationary_anchor=anchor,
transition_frames=50,
)
assert transition_frames == 50
np.testing.assert_allclose(sequence[0], anchor, atol=1e-7)
np.testing.assert_allclose(sequence[49], task[0], atol=1e-7)
assert abs(float(sequence[1, 0] - anchor[0])) < 5e-5
assert abs(float(task[0, 0] - sequence[48, 0])) < 5e-5
def test_source_sequence_uses_30_20_10_rtc_and_phase_labels(monkeypatch):
dataset = FullDuplexAudioCommandDataset.__new__(FullDuplexAudioCommandDataset)
dataset.records = [
{
"row_id": 7,
"filename": "test_motion",
"instruction": "test",
"audio_dir": "seed_audio/000007",
"motion_dir": Path("/unused/test_motion"),
"declared_frames": 45,
}
]
slices = tuple(
HiddenSlice(frame_id, Path(f"/unused/{frame_id}"), 0, 1)
for frame_id in (0, 20, 40)
)
dataset.audio_catalog = {
"seed_audio/000007": (
AudioSource("seed_audio/000007/voice", slices),
)
}
dataset.white_noise_hidden = tuple(
torch.full((4, 8), float(value)) for value in (9, 10, 11)
)
dataset.fixed_voice_per_record = False
dataset.action_chunk_size = 30
dataset.action_exec_horizon = 20
dataset.prefix_frames = 10
dataset.max_motion_steps = 0
dataset.initial_idle_max_steps = 2
dataset.stationary_blend_frames = 20
motion = np.arange(45 * 36, dtype=np.float32).reshape(45, 36)
monkeypatch.setattr(dataset, "_load_motion", lambda path: motion)
monkeypatch.setattr(
FullDuplexAudioCommandDataset,
"_load_slice",
staticmethod(lambda item: torch.full((4, 8), float(item.frame_id))),
)
monkeypatch.setattr(
dataset,
"_sample_stationary_sequence",
lambda phase, frame_count, anchor_pose: (
np.repeat(
np.asarray(anchor_pose, dtype=np.float32)[None, :],
frame_count,
axis=0,
),
f"test-{phase}",
),
)
monkeypatch.setattr(
dataset,
"_sample_stationary_anchor",
lambda task_start_pose: np.asarray(task_start_pose, dtype=np.float32),
)
monkeypatch.setattr(
dataset,
"_build_task_sequence",
lambda task_motion, stationary_anchor: (
np.asarray(task_motion, dtype=np.float32),
1,
),
)
monkeypatch.setattr("random.randint", lambda low, high: 2)
monkeypatch.setattr("random.randrange", lambda stop: 1)
source = dataset[0]
assert source["white_noise_start_index"] == 1
assert source["idle_steps"] == 2
assert source["listen_steps"] == 3
assert source["motion_steps"] == 2
assert source["action_idle_steps"] == 40
assert source["action_listen_steps"] == 60
assert source["action_motion_steps"] == 34
assert [step["phase"] for step in source["steps"]].count("idle") == 40
assert [step["phase"] for step in source["steps"]].count("listen") == 60
assert [step["phase"] for step in source["steps"]].count("motion") == 34
assert [
index
for index, step in enumerate(source["steps"])
if step["update_semantic_cache"]
] == [0, 20, 40, 60, 80, 100, 120]
assert all(
step["control_label"] == IDLE_TOKEN_ID
for step in source["steps"][:40]
)
assert all(
step["control_label"] == LISTEN_TOKEN_ID
for step in source["steps"][40:100]
)
assert all(
step["control_label"] == MOTION_TOKEN_ID
for step in source["steps"][100:]
)
# A 30-frame target crossing a state boundary now keeps one control label
# per future frame. It is no longer falsely described as all-idle or
# all-listen by the scalar current-window token.
np.testing.assert_array_equal(
source["steps"][20]["action_control_labels"],
np.asarray(
[IDLE_TOKEN_ID] * 19 + [LISTEN_TOKEN_ID] * 11,
dtype=np.int64,
),
)
np.testing.assert_array_equal(
source["steps"][80]["action_control_labels"],
np.asarray(
[LISTEN_TOKEN_ID] * 19 + [MOTION_TOKEN_ID] * 11,
dtype=np.int64,
),
)
assert source["steps"][40]["control_transition"] is True
assert source["steps"][100]["control_transition"] is True
assert source["steps"][41]["control_transition"] is False
assert [
float(source["steps"][index]["vlm_hidden_states"][0, 0])
for index in (0, 20, 40, 60, 80, 100, 120)
] == [10.0, 11.0, 0.0, 20.0, 40.0, 9.0, 10.0]
assert "vlm_hidden_states" not in source["steps"][1]
first_motion = source["steps"][100]
np.testing.assert_array_equal(first_motion["actions"], motion[1:31])
np.testing.assert_array_equal(first_motion["states"][0], motion[0])
second_motion = source["steps"][101]
np.testing.assert_array_equal(second_motion["actions"], motion[2:32])
np.testing.assert_array_equal(second_motion["states"][0], motion[1])
np.testing.assert_array_equal(
second_motion["action_reference_states"][0],
motion[1],
)
final_motion = source["steps"][-1]
np.testing.assert_array_equal(final_motion["actions"][:11], motion[34:45])
np.testing.assert_array_equal(
final_motion["actions"][11:],
np.repeat(motion[44:45], 19, axis=0),
)
assert final_motion["action_mask"].sum() == 11
def test_textop_rtc_first_delta_uses_current_state_before_future_horizon():
actions = np.zeros((30, 36), dtype=np.float32)
actions[:, 29] = np.arange(10, 40, dtype=np.float32)
actions[:, 32] = 1.0
observation_state = np.zeros((1, 36), dtype=np.float32)
observation_state[0, 29] = 29.0
observation_state[0, 32] = 1.0
action_reference_state = np.zeros((1, 36), dtype=np.float32)
action_reference_state[0, 29] = 9.0
action_reference_state[0, 32] = 1.0
transformed = SeedPosttrainRepackTransform(
action_chunk_size=30,
rtc_prefix_frames=10,
textop_actions=True,
)(
{
"actions": actions,
"action_mask": np.ones(30, dtype=np.float32),
"states": observation_state,
"action_reference_states": action_reference_state,
}
)
assert transformed["actions"].shape == (30, 39)
assert transformed["actions"][0, 29] == pytest.approx(1.0)
def test_dense_action_only_steps_skip_qwen_audio_transform():
class Stage:
def __init__(self, name, calls):
self.name = name
self.calls = calls
def __call__(self, data, **kwargs):
del kwargs
self.calls.append(self.name)
output = dict(data)
if self.name == "field":
output["actions_mask"] = output.pop("action_mask")
if self.name == "model":
assert "audio_waveform" in output
output["stream_input_ids"] = torch.ones(3, dtype=torch.long)
return output
class Transform:
def __init__(self):
self.calls = []
self.repack = Stage("repack", self.calls)
self.field = Stage("field", self.calls)
self.model = Stage("model", self.calls)
def __call__(self, data, **kwargs):
data = self.repack(data, **kwargs)
data = self.field(data, **kwargs)
return self.model(data, **kwargs)
base = {
"source_id": "source-a",
"source_audio_key": "audio-a",
"phase": "listen",
"control_label": LISTEN_TOKEN_ID,
"control_token": "</listen>",
"frame_id": 0,
"semantic_frame_id": 0,
"semantic_window_index": 0,
"actions": np.zeros((30, 39), dtype=np.float32),
"action_mask": np.ones((30, 39), dtype=np.float32),
"states": np.zeros((1, 39), dtype=np.float32),
"progress": np.zeros(5, dtype=np.float32),
"future_progress": np.zeros((30, 5), dtype=np.float32),
"motion_duration_target_seconds": 4.25,
"motion_duration_target_mask": False,
}
update = {
**base,
"step_index": 0,
"action_substep": 0,
"update_semantic_cache": True,
"audio_waveform": np.zeros(9_600, dtype=np.float32),
"motion_duration_target_mask": True,
}
reuse = {
**base,
"step_index": 1,
"action_substep": 1,
"update_semantic_cache": False,
}
class RawDataset:
def __len__(self):
return 1
def __getitem__(self, index):
assert index == 0
return {"source_id": "source-a", "steps": [update, reuse]}
def estimated_stream_steps(self, index):
assert index == 0
return 2
transform = Transform()
dataset = FullDuplexTransformedDataset(RawDataset(), transform)
source = dataset[0]
assert transform.calls == ["repack", "field", "model", "repack", "field"]
assert "stream_input_ids" in source["steps"][0]
assert "stream_input_ids" not in source["steps"][1]
assert source["steps"][0]["motion_duration_target_seconds"] == 4.25
assert source["steps"][0]["motion_duration_target_mask"] is True
assert source["steps"][1]["motion_duration_target_mask"] is False
def test_real_seed_repack_preserves_motion_duration_supervision():
class IdentityStage:
def __call__(self, data, **kwargs):
del kwargs
return data
class Transform:
def __init__(self):
self.repack = SeedPosttrainRepackTransform(
action_chunk_size=30,
rtc_prefix_frames=10,
textop_actions=False,
pad_action_dim=39,
pad_state_dim=39,
)
self.field = IdentityStage()
self.model = IdentityStage()
def __call__(self, data, **kwargs):
data = self.repack(data, **kwargs)
data = self.field(data, **kwargs)
return self.model(data, **kwargs)
raw_step = {
"source_id": "source-duration",
"source_audio_key": "audio-duration",
"phase": "motion",
"control_label": MOTION_TOKEN_ID,
"control_token": "</motion>",
"control_transition": True,
"action_control_labels": np.full(30, MOTION_TOKEN_ID),
"update_semantic_cache": True,
"step_index": 0,
"frame_id": 0,
"semantic_frame_id": 0,
"semantic_window_index": 0,
"action_substep": 0,
"turn_index": 0,
"actions": np.zeros((30, 39), dtype=np.float32),
"action_mask": np.ones((30, 39), dtype=np.float32),
"states": np.zeros((1, 39), dtype=np.float32),
"progress": np.zeros(5, dtype=np.float32),
"future_progress": np.zeros((30, 5), dtype=np.float32),
"instruction": "walk",
"motion_duration_target_seconds": 6.5,
"motion_duration_target_mask": True,
"latest_instruction_text": "walk",
"latest_instruction_target_mask": True,
}
class RawDataset:
def __len__(self):
return 1
def __getitem__(self, index):
assert index == 0
return {"source_id": "source-duration", "steps": [raw_step]}
def estimated_stream_steps(self, index):
assert index == 0
return 1
source = FullDuplexTransformedDataset(RawDataset(), Transform())[0]
assert source["steps"][0]["motion_duration_target_seconds"] == 6.5
assert source["steps"][0]["motion_duration_target_mask"] is True
assert source["steps"][0]["latest_instruction_text"] == "walk"
assert source["steps"][0]["latest_instruction_target_mask"] is True
assert source["steps"][0]["instruction_memory_text"] == ""
assert source["steps"][0]["instruction_memory_slot"] == ""
assert source["steps"][0]["instruction_memory_target_mask"] is False
def test_continuous_background_indices_are_adjacent_without_wrap(monkeypatch):
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.records = [
{
"row_id": 7,
"filename": "test_motion",
"instruction": "test",
"audio_dir": "seed_audio/000007",
"motion_dir": Path("/unused/test_motion"),
"declared_frames": 45,
}
]
slices = tuple(
HiddenSlice(frame_id, Path(f"/unused/{frame_id}"), 0, 1)
for frame_id in (0, 20, 40)
)
voice = AudioSource("seed_audio/000007/voice", slices)
dataset.audio_catalog = {"seed_audio/000007": (voice,)}
dataset.continuous_background = True
dataset.background_assignments = {
voice.source_key: SourceBackgroundAssignment(
source_key=voice.source_key,
background_key="sessions/office",
listen_start_index=100,
listen_window_count=3,
background_window_count=300,
initial_idle_max_steps=2,
max_motion_steps=3,
safety_steps=10,
)
}
dataset.white_noise_hidden = ()
dataset.fixed_voice_per_record = True
dataset.action_chunk_size = 30
dataset.action_exec_horizon = 20
dataset.prefix_frames = 10
dataset.max_motion_steps = 0
dataset.initial_idle_max_steps = 2
dataset.stationary_blend_frames = 20
motion = np.arange(45 * 36, dtype=np.float32).reshape(45, 36)
monkeypatch.setattr(dataset, "_load_motion", lambda path: motion)
monkeypatch.setattr(
FullDuplexAudioCommandDataset,
"_load_slice",
staticmethod(lambda item: torch.full((4, 8), float(item.frame_id))),
)
loaded_background_blocks = []
def load_background_block(source_key, start, count):
assert source_key == "sessions/office"
loaded_background_blocks.append((start, count))
return tuple(
torch.full((4, 8), float(index))
for index in range(start, start + count)
)
monkeypatch.setattr(
dataset,
"_load_background_window_block",
load_background_block,
)
monkeypatch.setattr(
dataset,
"_sample_stationary_sequence",
lambda phase, frame_count, anchor_pose: (
np.repeat(
np.asarray(anchor_pose, dtype=np.float32)[None, :],
frame_count,
axis=0,
),
f"test-{phase}",
),
)
monkeypatch.setattr(
dataset,
"_sample_stationary_anchor",
lambda task_start_pose: np.asarray(task_start_pose, dtype=np.float32),
)
monkeypatch.setattr(
dataset,
"_build_task_sequence",
lambda task_motion, stationary_anchor: (
np.asarray(task_motion, dtype=np.float32),
1,
),
)
monkeypatch.setattr("random.randint", lambda low, high: 2)
source = dataset[0]
assert loaded_background_blocks == [(98, 2), (103, 2)]
assert source["background_key"] == "sessions/office"
assert source["background_listen_start_index"] == 100
assert source["listen_steps"] == 3
assert source["motion_steps"] == 2
def test_overfit_voice_selection_is_stable_per_record(monkeypatch):
voices = tuple(
AudioSource(f"seed_audio/voice_{index}", ()) for index in range(4)
)
dataset = FullDuplexAudioCommandDataset.__new__(FullDuplexAudioCommandDataset)
dataset.fixed_voice_per_record = True
selected = [dataset._select_voice(voices, row_id=7) for _ in range(5)]
assert selected == [voices[3]] * 5
dataset.fixed_voice_per_record = False
monkeypatch.setattr("random.choice", lambda values: values[1])
assert dataset._select_voice(voices, row_id=7) is voices[1]
def test_broken_tts_duration_outliers_are_filtered_before_sampling():
def voice(name: str, windows: int) -> AudioSource:
return AudioSource(
name,
(),
waveform_path=Path(f"/unused/{name}.wav"),
waveform_count=windows,
)
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.max_voice_windows = 30
dataset.max_voice_median_ratio = 3.0
catalog = {
"normal-with-corruption": (
voice("short-a", 7),
voice("short-b", 8),
voice("broken", 196),
),
# Defensive fallback keeps a custom long-command directory usable.
"all-long": (voice("long-a", 35), voice("long-b", 40)),
}
filtered, removed = dataset._filter_voice_duration_outliers(catalog)
assert [item.source_key for item in filtered["normal-with-corruption"]] == [
"short-a",
"short-b",
]
assert [item.source_key for item in filtered["all-long"]] == ["long-a"]
assert removed == 2
def test_length_estimate_uses_fixed_voice_and_converts_metadata_fps():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.records = [
{
"row_id": 1,
"audio_dir": "seed_audio/one",
"declared_frames": 580,
}
]
dataset.audio_catalog = {
"seed_audio/one": (
AudioSource("voice-a", (), packed_path=Path("a"), packed_count=7),
AudioSource("voice-b", (), packed_path=Path("b"), packed_count=10),
)
}
dataset.fixed_voice_per_record = True
dataset.motion_fps = 50
dataset.motion_metadata_fps = 120
dataset.stationary_blend_frames = 20
dataset.prefix_frames = 10
dataset.action_exec_horizon = 20
dataset.initial_idle_max_steps = 5
dataset.max_motion_steps = 0
dataset.max_stream_steps = 1923
# 580 metadata frames -> 242 exported 50 Hz frames. With the 19-frame
# transition there are 250 valid dense motion starts.
assert dataset._estimated_runtime_motion_frames(dataset.records[0]) == 242
assert dataset.estimated_stream_steps(0) == (2 + 10) * 20 + 250
assert dataset._record_max_stream_steps(dataset.records[0]) == 5 + 10 + 13
def test_validation_source_augmentation_is_repeatable_and_restores_rng():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.records = [{"row_id": 17}]
dataset.split = "val"
dataset.split_seed = 42
dataset.deterministic_eval = True
dataset._build_source = lambda index: {
"index": index,
"voice": random.randrange(8),
"idle": random.randint(0, 5),
"anchor": random.random(),
}
random.seed(1234)
state_before = random.getstate()
first = dataset[0]
assert random.getstate() == state_before
second = dataset[0]
assert first == second
assert random.getstate() == state_before
def test_dataset_prefers_compact_source_indexes_and_reads_one_wav(
tmp_path,
):
source_dir = (
tmp_path
/ "audio"
/ "qwen3_tts_seed_audio_command"
/ "audio_command_text"
/ "sources"
)
source_path = (
source_dir
/ "seed_audio"
/ "000007"
/ "voice.safetensors"
)
source_path.parent.mkdir(parents=True)
hidden = torch.arange(32, dtype=torch.bfloat16).reshape(8, 4)
save_file(
{
"hidden_states": hidden,
"offsets": torch.tensor([0, 3, 8], dtype=torch.int64),
"frame_ids": torch.tensor([0, 20], dtype=torch.int64),
},
source_path,
metadata={
"format": HFD_AUDIO_SOURCE_FORMAT,
"encoder_contract": HFD_ONLINE_ENCODER_CONTRACT,
"encoder_batch_size": str(HFD_ONLINE_ENCODER_BATCH_SIZE),
"encoder_dtype": HFD_ONLINE_ENCODER_DTYPE,
"attention_implementation": HFD_ONLINE_ENCODER_ATTENTION,
"instruction_sha1": hashlib.sha1(
AUDIO_COMMAND_STREAMING_PROMPT.encode("utf-8")
).hexdigest(),
"count": "2",
"window_seconds": "0.600000000",
"step_frames": "20",
"motion_fps": "50",
"sampling_rate": "16000",
},
)
index_path = source_dir / "index.ws1.rank00.sqlite"
with sqlite3.connect(index_path) as conn:
conn.execute(
"CREATE TABLE sources ("
"key TEXT PRIMARY KEY, path TEXT NOT NULL, count INTEGER NOT NULL, "
"window_seconds REAL NOT NULL, step_frames INTEGER NOT NULL, "
"motion_fps INTEGER NOT NULL, sampling_rate INTEGER NOT NULL)"
)
conn.execute(
"INSERT INTO sources VALUES (?, ?, ?, ?, ?, ?, ?)",
(
"seed_audio/000007/voice",
"seed_audio/000007/voice.safetensors",
2,
0.6,
20,
50,
16000,
),
)
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.hidden_root = tmp_path
dataset.source_name = "qwen3_tts_seed_audio_command"
dataset.task_name = "audio_command_text"
dataset.audio_window_seconds = 0.6
dataset.audio_sample_step_frames = 20
dataset.motion_fps = 50
dataset.audio_sampling_rate = 16000
catalog = dataset._load_audio_catalog()
voice = catalog["seed_audio/000007"][0]
windows = dataset._load_voice_hidden(voice)
assert dataset.hidden_layout == "source"
assert voice.packed_path == source_path.resolve()
assert voice.clips == ()
assert voice.window_count == 2
assert torch.equal(windows[0], hidden[:3])
assert torch.equal(windows[1], hidden[3:])
assert windows[0].untyped_storage().data_ptr() == (
windows[1].untyped_storage().data_ptr()
)
def test_stationary_motion_pool_is_kinematically_gated_and_blended(monkeypatch):
anchor = np.zeros(36, dtype=np.float32)
anchor[29:32] = [1.0, 2.0, 0.8]
anchor[32] = 1.0
natural = np.repeat(anchor[None, :], 80, axis=0)
natural[:, 0] = np.sin(np.linspace(0.0, 2.0 * np.pi, 80)) * 0.2
natural[:, 29] = 4.0 + np.sin(np.linspace(0.0, 2.0 * np.pi, 80)) * 0.02
natural[:, 30] = -3.0
natural[:, 31] = 0.92 + np.sin(np.linspace(0.0, np.pi, 80)) * 0.01
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
candidate = StationaryMotionSource(
"safe-neutral",
Path("/unused/safe-neutral"),
len(natural),
"neutral_idle",
)
dataset.stationary_motion_pools = {
"idle": (candidate,),
"listen": (candidate,),
}
dataset.stationary_blend_frames = 10
monkeypatch.setattr(dataset, "_load_motion", lambda path: natural)
monkeypatch.setattr("random.randint", lambda low, high: 0)
sequence, name = dataset._sample_stationary_sequence(
"idle",
frame_count=40,
anchor_pose=anchor,
)
assert name == "safe-neutral"
np.testing.assert_allclose(sequence[0], anchor, atol=1e-6)
np.testing.assert_allclose(sequence[-1], anchor, atol=1e-6)
assert np.max(np.abs(sequence[10:-10, 0] - anchor[0])) > 0.05
np.testing.assert_allclose(
sequence[10, 29:31],
anchor[29:31] + natural[10, 29:31] - natural[0, 29:31],
atol=1e-6,
)
np.testing.assert_allclose(sequence[10, 31], natural[10, 31], atol=1e-6)
np.testing.assert_allclose(
np.linalg.norm(sequence[:, 32:36], axis=-1),
np.ones(40),
atol=1e-6,
)
task_start = anchor.copy()
task_start[29:32] = [7.0, 8.0, 0.35]
sampled_anchor = dataset._sample_stationary_anchor(task_start)
np.testing.assert_allclose(sampled_anchor[29:31], task_start[29:31], atol=1e-6)
np.testing.assert_allclose(sampled_anchor[31], natural[0, 31], atol=1e-6)
np.testing.assert_allclose(sampled_anchor[:29], natural[0, :29], atol=1e-6)
locomoting = natural.copy()
locomoting[:, 29] += np.linspace(0.0, 1.0, len(locomoting))
assert dataset._stationary_motion_is_safe(natural)
assert not dataset._stationary_motion_is_safe(locomoting)
def test_v24_idle_uses_motion_residual_instead_of_unrelated_absolute_pose(
monkeypatch,
):
anchor = np.zeros(36, dtype=np.float32)
anchor[0] = 0.20
anchor[31] = 0.82
anchor[32] = 1.0
recorded = np.repeat(anchor[None, :], 60, axis=0)
# The recorded clip is quiet, but its absolute arm pose is unrelated to
# this session's current pose. Only its subtle joint-1 residual is useful.
recorded[:, 0] = 1.50
recorded[:, 1] = 0.05 * np.sin(
np.linspace(0.0, 2.0 * np.pi, len(recorded), dtype=np.float32)
)
source = StationaryMotionSource(
"quiet-different-arm-pose",
Path("/unused/quiet-different-arm-pose"),
len(recorded),
"neutral_idle",
)
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.stationary_motion_pools = {
"idle": (source,),
"listen": (source,),
}
dataset.stationary_blend_frames = 5
dataset.stationary_heading_alignment = False
dataset.safe_stationary_anchors = False
monkeypatch.setattr(dataset, "_load_motion", lambda path: recorded)
monkeypatch.setattr("random.randint", lambda low, high: 0)
dataset.stationary_idle_residual_retargeting = False
legacy, _ = dataset._sample_stationary_sequence(
"idle",
frame_count=60,
anchor_pose=anchor,
)
assert float(np.max(np.abs(legacy[:, 0] - anchor[0]))) > 1.0
dataset.stationary_idle_residual_retargeting = True
retargeted, _ = dataset._sample_stationary_sequence(
"idle",
frame_count=60,
anchor_pose=anchor,
)
np.testing.assert_allclose(retargeted[:, 0], anchor[0], atol=1e-6)
assert float(np.max(np.abs(retargeted[:, 1] - anchor[1]))) > 0.04
np.testing.assert_allclose(retargeted[0], anchor, atol=1e-6)
np.testing.assert_allclose(retargeted[-1], anchor, atol=1e-6)
def test_v24_idle_gate_rejects_jump_cuts_and_large_gestures():
clean = np.zeros((100, 36), dtype=np.float32)
clean[:, 31] = 0.82
clean[:, 32] = 1.0
clean[:, 0] = 0.08 * np.sin(
np.linspace(0.0, 2.0 * np.pi, len(clean), dtype=np.float32)
)
jump_cut = clean.copy()
jump_cut[50:, 3] += 0.20
gesture = clean.copy()
gesture[:, 4] = np.linspace(0.0, 0.60, len(gesture), dtype=np.float32)
assert FullDuplexAudioCommandDataset._idle_motion_is_quiet_and_continuous(
clean
)
assert not FullDuplexAudioCommandDataset._idle_motion_is_quiet_and_continuous(
jump_cut
)
assert not FullDuplexAudioCommandDataset._idle_motion_is_quiet_and_continuous(
gesture
)
def test_safe_stationary_audit_rejects_static_whole_body_outlier(monkeypatch):
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.motion_fps = 50
dataset.stationary_motion_pools = {"idle": (), "listen": ()}
sources = tuple(
StationaryMotionSource(
name,
Path(f"/unused/{name}"),
100,
"neutral_idle",
)
for name in ("neutral-a", "neutral-b", "corrupt-static")
)
dataset.stationary_motion_pools["idle"] = sources
motions = {}
for source in sources:
motion = np.zeros((100, 36), dtype=np.float32)
motion[:, 31] = 0.82
motion[:, 32] = 1.0
motions[str(source.motion_dir)] = motion
motions["/unused/neutral-b"][:, 0] = 0.03
# Static alone is not sufficient: this mimics the real malformed idle clip
# whose arm joints are many robust deviations away from the neutral pool.
motions["/unused/corrupt-static"][:, 22:29] = 2.0
monkeypatch.setattr(
dataset,
"_load_motion",
lambda path: motions[str(path)],
)
dataset._initialise_safe_stationary_anchors()
assert "/unused/corrupt-static" not in dataset._safe_idle_motion_paths
assert dataset.stationary_audit_summary["accepted_clips"] == 2
assert dataset.stationary_audit_summary["rejected_clips"] == 1
assert dataset.stationary_audit_summary["anchor_frames"] > 0
query = motions["/unused/neutral-b"][50].copy()
query[29:31] = [4.0, -2.0]
anchor, _ = dataset._nearest_safe_stationary_anchor(query)
np.testing.assert_allclose(anchor[:29], query[:29], atol=1e-6)
np.testing.assert_allclose(anchor[29:31], query[29:31], atol=1e-6)
def test_stationary_entry_blends_from_executed_pose_to_safe_anchor(monkeypatch):
anchor = np.zeros(36, dtype=np.float32)
anchor[31] = 0.82
anchor[32] = 1.0
entry = anchor.copy()
entry[0] = 0.8
natural = np.repeat(anchor[None, :], 80, axis=0)
natural[:, 1] = 0.2
source = StationaryMotionSource(
"listen",
Path("/unused/listen"),
len(natural),
"listening",
)
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.stationary_motion_pools = {"idle": (source,), "listen": (source,)}
dataset.stationary_blend_frames = 10
dataset.turn_transition_frames = 50
dataset.stationary_heading_alignment = True
dataset.safe_stationary_anchors = False
monkeypatch.setattr(dataset, "_load_motion", lambda path: natural)
monkeypatch.setattr("random.randint", lambda low, high: 0)
sequence, _ = dataset._sample_stationary_sequence(
"listen",
frame_count=80,
anchor_pose=anchor,
blend_frames=50,
entry_pose=entry,
)
np.testing.assert_allclose(sequence[0], entry, atol=1e-6)
np.testing.assert_allclose(sequence[-1], anchor, atol=1e-6)
assert float(sequence[25, 0]) < float(entry[0])
def test_stationary_motion_heading_is_reexpressed_in_current_anchor(monkeypatch):
anchor = np.zeros(36, dtype=np.float32)
anchor[29:32] = [2.0, -1.0, 0.8]
anchor_yaw = 0.5 * np.pi
anchor[32:36] = rpy_to_quat_wxyz(0.0, 0.0, anchor_yaw)
candidate_motion = np.repeat(anchor[None, :], 40, axis=0)
candidate_motion[:, 29:31] = [8.0, 4.0]
recorded_yaw = -0.5 * np.pi
relative_yaw = np.linspace(0.0, 0.1, 40, dtype=np.float32)
candidate_motion[:, 32:36] = np.stack(
[
rpy_to_quat_wxyz(0.0, 0.0, recorded_yaw + float(offset))
for offset in relative_yaw
]
)
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
source = StationaryMotionSource(
"listen-facing-another-way",
Path("/unused/listen-facing-another-way"),
len(candidate_motion),
"listening",
)
dataset.stationary_motion_pools = {
"idle": (source,),
"listen": (source,),
}
dataset.stationary_blend_frames = 5
dataset.stationary_heading_alignment = True
monkeypatch.setattr(dataset, "_load_motion", lambda path: candidate_motion)
monkeypatch.setattr("random.randint", lambda low, high: 0)
sequence, _ = dataset._sample_stationary_sequence(
"listen",
frame_count=40,
anchor_pose=anchor,
)
output_yaw = np.unwrap(
np.asarray(
[quat_to_yaw_wxyz(frame[32:36]) for frame in sequence]
)
)
assert abs(float(wrap_angle(output_yaw[0] - anchor_yaw))) < 1e-6
assert abs(float(wrap_angle(output_yaw[-1] - anchor_yaw))) < 1e-6
# The candidate's small relative turn is preserved, while its unrelated
# 180-degree recording orientation is removed.
assert np.max(np.abs(output_yaw - anchor_yaw)) < 0.11
def test_dense_stream_rows_carry_two_source_local_context_tokens():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.action_chunk_size = 3
dataset.prefix_frames = 1
sequence = np.zeros((8, 36), dtype=np.float32)
sequence[:, 0] = np.arange(8, dtype=np.float32)
sequence[:, 29] = np.arange(8, dtype=np.float32) * 0.1
sequence[:, 32] = 1.0
rows = dataset._stream_action_rows(sequence, start=4)
actions, mask, state, reference, context, context_reference = rows
np.testing.assert_allclose(actions, sequence[5:8])
np.testing.assert_array_equal(mask, np.ones(3, dtype=np.float32))
np.testing.assert_allclose(state, sequence[4])
np.testing.assert_allclose(reference, sequence[4])
np.testing.assert_allclose(context, sequence[2:4 + 1][1:])
np.testing.assert_allclose(context_reference, sequence[2])
start_rows = dataset._stream_action_rows(sequence, start=0)
np.testing.assert_allclose(start_rows[4], np.repeat(sequence[0:1], 2, axis=0))
np.testing.assert_allclose(start_rows[5], sequence[0])
def test_task_motion_transition_starts_from_upright_anchor_without_jump():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.stationary_blend_frames = 20
anchor = np.zeros(36, dtype=np.float32)
anchor[0] = -0.5
anchor[29:32] = [1.0, 2.0, 0.8]
anchor[32] = 1.0
task = np.repeat(anchor[None, :], 60, axis=0)
task[:, 0] = 0.75
task[:, 31] = 0.5
sequence, transition_frames = dataset._build_task_sequence(
task,
stationary_anchor=anchor,
)
assert transition_frames == 20
assert len(sequence) == len(task) + transition_frames - 1
np.testing.assert_allclose(sequence[0], anchor, atol=1e-6)
np.testing.assert_allclose(sequence[transition_frames - 1], task[0], atol=1e-6)
np.testing.assert_allclose(sequence[transition_frames], task[1], atol=1e-6)
assert np.max(np.linalg.norm(np.diff(sequence[:, :29], axis=0), axis=1)) < 0.11
def test_dynamic_task_entry_blends_into_moving_prefix_without_phase_restart():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.stationary_blend_frames = 20
dataset.dynamic_task_entry_blend = True
anchor = np.zeros(36, dtype=np.float32)
anchor[31] = 0.82
anchor[32] = 1.0
task = np.repeat(anchor[None, :], 80, axis=0)
task[:, 0] = np.linspace(0.6, 1.4, len(task), dtype=np.float32)
sequence, transition_frames = dataset._build_task_sequence(
task,
stationary_anchor=anchor,
)
assert transition_frames == 20
assert len(sequence) == len(task)
np.testing.assert_allclose(sequence[0], anchor, atol=1e-6)
np.testing.assert_allclose(sequence[19], task[19], atol=1e-6)
# At the crossfade boundary the real trajectory simply continues; it never
# jumps back to primitive frame zero or restarts its phase.
np.testing.assert_allclose(sequence[20], task[20], atol=1e-6)
np.testing.assert_allclose(
sequence[20, :29] - sequence[19, :29],
task[20, :29] - task[19, :29],
atol=1e-6,
)
np.testing.assert_array_equal(
dataset._task_original_frame_indices(
sequence_frames=len(sequence),
motion_frames=len(task),
transition_frames=transition_frames,
),
np.arange(len(task)),
)
def test_task_motion_is_rigidly_aligned_to_stationary_anchor_heading():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
dataset.stationary_blend_frames = 5
anchor = np.zeros(36, dtype=np.float32)
anchor[29:32] = [10.0, -3.0, 0.8]
anchor_yaw = -0.5 * np.pi
anchor[32:36] = rpy_to_quat_wxyz(0.0, 0.0, anchor_yaw)
task = np.zeros((4, 36), dtype=np.float32)
task[:, :29] = np.arange(4, dtype=np.float32)[:, None]
task[:, 29] = 1.0
task[:, 30] = 2.0 + np.arange(4, dtype=np.float32)
task[:, 31] = 0.8 + 0.05 * np.arange(4, dtype=np.float32)
task_yaw = 0.5 * np.pi
yaw_progress = 0.1 * np.arange(4, dtype=np.float32)
task[:, 32:36] = np.stack(
[
rpy_to_quat_wxyz(0.0, 0.0, float(task_yaw + offset))
for offset in yaw_progress
]
)
sequence, transition_frames = dataset._build_task_sequence(
task,
stationary_anchor=anchor,
)
aligned = sequence[transition_frames - 1 :]
np.testing.assert_allclose(sequence[0], anchor, atol=1e-6)
np.testing.assert_allclose(aligned[0, 29:32], anchor[29:32], atol=1e-6)
np.testing.assert_allclose(aligned[:, :29], task[:, :29], atol=1e-6)
np.testing.assert_allclose(
aligned[:, 31] - aligned[0, 31],
task[:, 31] - task[0, 31],
atol=1e-6,
)
aligned_yaw = quat_to_yaw_wxyz(aligned[:, 32:36])
np.testing.assert_allclose(
wrap_angle(aligned_yaw - anchor_yaw),
yaw_progress,
atol=1e-6,
)
original_local_delta = local_delta_from_world(
task[:-1, 29:32],
task[1:, 29:32],
task[:-1, 32:36],
)
aligned_local_delta = local_delta_from_world(
aligned[:-1, 29:32],
aligned[1:, 29:32],
aligned[:-1, 32:36],
)
np.testing.assert_allclose(
aligned_local_delta,
original_local_delta,
atol=1e-6,
)
np.testing.assert_allclose(
np.linalg.norm(aligned[:, 32:36], axis=-1),
np.ones(len(aligned)),
atol=1e-6,
)
def test_kv_cache_is_shared_only_inside_one_source():
memory = StreamingCausalMemory(
input_dim=8,
hidden_dim=16,
num_layers=2,
num_heads=4,
dropout=0.0,
max_cache_tokens=32,
)
cache_a = memory.begin_source(["source-a"])
pooled = torch.randn(1, 8)
_, cache_a, _ = memory(
pooled, cache_a, source_ids=["source-a"], update_cache=True
)
_, cache_a, _ = memory(
pooled, cache_a, source_ids=["source-a"], update_cache=True
)
assert cache_a.sequence_length == 2
# False is reserved for a trainer-inserted padding row after the source has
# ended; it must not insert the placeholder token into semantic history.
_, padded_cache, _ = memory(
torch.randn(1, 8), cache_a, source_ids=["source-a"], update_cache=False
)
assert padded_cache.sequence_length == 2
for before, after in zip(cache_a.keys, padded_cache.keys):
assert before is after
with pytest.raises(RuntimeError, match="across sources"):
memory(pooled, cache_a, source_ids=["source-b"], update_cache=True)
cache_b = memory.begin_source(["source-b"])
assert cache_b.sequence_length == 0
def test_batched_kv_cache_tracks_mixed_active_and_padded_sources():
memory = StreamingCausalMemory(
input_dim=8,
hidden_dim=16,
num_layers=2,
num_heads=4,
dropout=0.0,
max_cache_tokens=32,
).eval()
cache = memory.begin_source(["source-a", "source-b"])
_, cache, _ = memory(
torch.randn(2, 8),
cache,
source_ids=["source-a", "source-b"],
update_cache=[True, True],
)
assert cache.sequence_lengths == (1, 1)
# A has ended while B still has an active audio window. The shared physical
# cache grows once, but the new position is invalid for A and valid for B.
_, cache, _ = memory(
torch.randn(2, 8),
cache,
source_ids=["source-a", "source-b"],
update_cache=[False, True],
)
assert cache.sequence_lengths == (1, 2)
assert cache.valid_mask is not None
assert cache.valid_mask.tolist() == [[True, False], [True, True]]
with pytest.raises(RuntimeError, match="different source lengths"):
_ = cache.sequence_length
before_keys = cache.keys
_, padded_cache, _ = memory(
torch.randn(2, 8),
cache,
source_ids=["source-a", "source-b"],
update_cache=[False, False],
)
assert padded_cache.sequence_lengths == (1, 2)
assert all(before is after for before, after in zip(before_keys, padded_cache.keys))
def test_trainer_stream_forward_is_a_real_two_source_batch():
config = FullDuplexPsi0ModelConfig(
streaming_qwen_enabled=False,
action_dim=3,
action_chunk_size=30,
action_exec_horizon=20,
observation_horizon=1,
odim=3,
n_conditions=0,
hidden_dim=48,
num_blocks=1,
dim_feedforward=64,
nhead=4,
view_feature_dim=8,
use_future_progress_embedding=False,
rtc=True,
max_delay=11,
streaming_memory_dim=16,
streaming_memory_layers=1,
streaming_memory_heads=4,
streaming_max_cache_tokens=8,
)
model = FullDuplexPsi0Model(config, vlm_model=None).eval()
captured_timesteps = []
original_forward = model.forward
def capture_forward(*args, **kwargs):
captured_timesteps.append(kwargs["timestep"].detach().clone())
return original_forward(*args, **kwargs)
model.forward = capture_forward
trainer = PosttrainFullDuplexTrainer.__new__(PosttrainFullDuplexTrainer)
trainer.device = torch.device("cpu")
trainer.rtc_train_overlap = 10
trainer.train_diffusion_steps = 1_000
trainer.loss_w = torch.ones(3)
trainer.loss_group_w = torch.ones(3)
class FixedSampler:
def sample_like(self, actions):
return SimpleNamespace(
sigmas=torch.tensor([0.25, 0.75], dtype=torch.float32),
noise=torch.zeros_like(actions),
)
def make_step(
source_id: str,
*,
listen: bool,
value: float,
step_index: int,
):
return {
"source_id": source_id,
"step_index": step_index,
"vlm_hidden_states": torch.full((5, 8), value),
"attention_mask": torch.ones(5, dtype=torch.long),
"actions": torch.full((30, 3), value),
"actions_mask": torch.ones(30, 3),
"states": torch.zeros(1, 3),
"progress": torch.zeros(5),
"future_progress": torch.zeros(30, 5),
"update_semantic_cache": listen,
"control_label": LISTEN_TOKEN_ID if listen else MOTION_TOKEN_ID,
}
source_ids = ["source-a", "source-b"]
cache = model.begin_source(source_ids)
losses, cache = trainer._forward_stream_batch(
model,
source_ids,
[
make_step("source-a", listen=True, value=1.0, step_index=0),
make_step("source-b", listen=True, value=2.0, step_index=0),
],
cache,
FixedSampler(),
)
assert losses["action_loss"].shape == (2,)
assert losses["control_loss"].shape == (2,)
assert torch.isfinite(losses["action_loss"]).all()
assert cache.sequence_lengths == (1, 1)
# The first source window has no previous prediction, hence no clean RTC
# prefix is leaked even though the sampler fallback requests delay 10.
assert torch.all(captured_timesteps[0][0] == 250.0)
assert torch.all(captured_timesteps[0][1] == 750.0)
losses, cache = trainer._forward_stream_batch(
model,
source_ids,
[
make_step("source-a", listen=False, value=3.0, step_index=1),
make_step("source-b", listen=True, value=4.0, step_index=1),
],
cache,
FixedSampler(),
)
assert losses["action_loss"].shape == (2,)
assert cache.sequence_lengths == (1, 2)
assert torch.all(captured_timesteps[1][:, :10] == 0.0)
@pytest.mark.parametrize("semantics_version", [12, 15, 19, 20])
def test_framewise_control_uses_versioned_future_mask_and_rtc_prefix(
semantics_version,
):
trainer = PosttrainFullDuplexTrainer.__new__(PosttrainFullDuplexTrainer)
trainer.device = torch.device("cpu")
trainer.rtc_train_overlap = 10
trainer.train_diffusion_steps = 1_000
trainer.loss_w = torch.ones(39)
trainer.loss_group_w = torch.ones(39)
trainer.cfg = SimpleNamespace(
train=SimpleNamespace(
full_duplex_semantics_version=semantics_version,
joint_velocity_loss_weight=0.10,
joint_acceleration_loss_weight=0.05,
root_continuity_loss_weight=0.10,
stationary_root_height_loss_weight=1.0,
),
model=SimpleNamespace(max_delay=11),
)
class FixedSampler:
def sample_like(self, actions):
return SimpleNamespace(
sigmas=torch.tensor([0.5], dtype=torch.float32),
noise=torch.zeros_like(actions),
delay=torch.tensor([10], dtype=torch.long),
)
class FakeModel:
def __init__(self):
self.captured_timestep = None
self._latest_reusable_condition = None
def __call__(self, **kwargs):
self.captured_timestep = kwargs["timestep"].detach().clone()
batch = kwargs["action_samples"].shape[0]
logits = torch.zeros((batch, 3), dtype=torch.float32)
probabilities = torch.softmax(logits, dim=-1)
condition = torch.zeros((batch, 1, 8), dtype=torch.float32)
mask = torch.ones((batch, 1), dtype=torch.long)
self._latest_reusable_condition = {
"condition_hidden_states": condition,
"condition_attention_mask": mask,
"control_logits": logits,
"control_probabilities": probabilities,
"control_condition_weights": probabilities,
"control_entropy": torch.full((batch,), float(np.log(3.0))),
"memory_token": condition,
"motion_duration_log1p_seconds": torch.zeros(
batch, dtype=torch.float32
),
}
return SimpleNamespace(
action=torch.zeros_like(kwargs["action_samples"]),
control_logits=logits,
control_probabilities=probabilities,
control_entropy=torch.full((batch,), float(np.log(3.0))),
motion_duration_log1p_seconds=torch.zeros(
batch, dtype=torch.float32
),
latest_instruction_loss=None,
latest_instruction_token_correct=None,
latest_instruction_token_count=None,
kv_cache=kwargs["kv_cache"],
)
actions = torch.zeros((30, 39), dtype=torch.float32)
actions[:, 36] = 2.0
step = {
"source_id": "source-a",
"step_index": 20,
"vlm_hidden_states": torch.zeros((5, 8)),
"attention_mask": torch.ones(5, dtype=torch.long),
"actions": actions,
"actions_mask": torch.ones((30, 39)),
"states": torch.zeros((1, 39)),
"smoothness_context_actions": torch.zeros((2, 39)),
"progress": torch.zeros(5),
"future_progress": torch.zeros((30, 5)),
"update_semantic_cache": True,
"control_label": LISTEN_TOKEN_ID,
"action_control_labels": torch.tensor(
[LISTEN_TOKEN_ID] * 15 + [MOTION_TOKEN_ID] * 15,
dtype=torch.long,
),
"control_transition": True,
"motion_duration_target_seconds": 0.0,
"motion_duration_target_mask": False,
"latest_instruction_text": "",
"latest_instruction_target_mask": False,
}
model = FakeModel()
cache = SimpleNamespace()
losses, _ = trainer._forward_stream_batch(
model,
["source-a"],
[step],
cache,
FixedSampler(),
)
# Historical v12/v15 regenerate all 30 frames and mask the cross-phase
# tail. v19 preserves the previous ten-frame RTC prefix and supervises the
# complete valid future horizon so that preceding windows learn that tail.
if semantics_version in {19, 20}:
assert torch.all(model.captured_timestep[:, :10] == 0.0)
assert torch.all(model.captured_timestep[:, 10:] == 500.0)
else:
assert torch.all(model.captured_timestep == 500.0)
assert losses["cross_phase_frames"].tolist() == [15.0]
assert losses["supervised_phase_frames"].tolist() == [
30.0 if semantics_version in {19, 20} else 15.0
]
assert losses["control_transition"].tolist() == [1.0]
assert float(losses["stationary_root_height_loss"].item()) > 0.0
@pytest.mark.parametrize("semantics_version", [18, 19, 20])
def test_completed_motion_progress_survives_cross_control_rtc_tail(
semantics_version,
):
trainer = PosttrainFullDuplexTrainer.__new__(PosttrainFullDuplexTrainer)
trainer.device = torch.device("cpu")
trainer.rtc_train_overlap = 10
trainer.train_diffusion_steps = 1_000
trainer.loss_w = torch.ones(39)
trainer.loss_group_w = torch.ones(39)
trainer.cfg = SimpleNamespace(
train=SimpleNamespace(
full_duplex_semantics_version=semantics_version,
joint_velocity_loss_weight=0.10,
joint_acceleration_loss_weight=0.05,
root_continuity_loss_weight=0.10,
stationary_root_height_loss_weight=1.0,
),
model=SimpleNamespace(max_delay=11),
)
class FixedSampler:
def sample_like(self, actions):
return SimpleNamespace(
sigmas=torch.tensor([0.5], dtype=torch.float32),
noise=torch.zeros_like(actions),
delay=torch.tensor([10], dtype=torch.long),
)
class FakeModel:
def __init__(self):
self.future_progress = None
self._latest_reusable_condition = None
def __call__(self, **kwargs):
self.future_progress = kwargs["future_progress"].detach().clone()
batch = kwargs["action_samples"].shape[0]
logits = torch.zeros((batch, 3), dtype=torch.float32)
probabilities = torch.softmax(logits, dim=-1)
condition = torch.zeros((batch, 1, 8), dtype=torch.float32)
mask = torch.ones((batch, 1), dtype=torch.long)
self._latest_reusable_condition = {
"condition_hidden_states": condition,
"condition_attention_mask": mask,
"control_logits": logits,
"control_probabilities": probabilities,
"control_condition_weights": probabilities,
"control_entropy": torch.full((batch,), float(np.log(3.0))),
"memory_token": condition,
"motion_duration_log1p_seconds": torch.zeros(
batch, dtype=torch.float32
),
}
return SimpleNamespace(
action=torch.zeros_like(kwargs["action_samples"]),
control_logits=logits,
control_probabilities=probabilities,
control_entropy=torch.full((batch,), float(np.log(3.0))),
motion_duration_log1p_seconds=torch.zeros(
batch, dtype=torch.float32
),
latest_instruction_loss=None,
latest_instruction_token_correct=None,
latest_instruction_token_count=None,
kv_cache=kwargs["kv_cache"],
)
future_progress = torch.zeros((30, 5), dtype=torch.float32)
future_progress[:15] = torch.tensor(
[0.8, 0.2, 0.8, -0.5, 0.5], dtype=torch.float32
)
step = {
"source_id": "source-a",
"step_index": 100,
"vlm_hidden_states": torch.zeros((5, 8)),
"attention_mask": torch.ones(5, dtype=torch.long),
"actions": torch.zeros((30, 39), dtype=torch.float32),
"actions_mask": torch.ones((30, 39), dtype=torch.float32),
"states": torch.zeros((1, 39), dtype=torch.float32),
"smoothness_context_actions": torch.zeros((2, 39), dtype=torch.float32),
"progress": torch.tensor([0.8, 0.2, 0.8, -0.5, 0.5]),
# The raw GT timeline changes to stationary p=0 in the tail.
"future_progress": future_progress,
"update_semantic_cache": True,
"control_label": MOTION_TOKEN_ID,
"action_control_labels": torch.tensor(
[MOTION_TOKEN_ID] * 15 + [IDLE_TOKEN_ID] * 15,
dtype=torch.long,
),
# This is the final motion window, not the next idle window.
"control_transition": False,
"motion_duration_target_seconds": 0.0,
"motion_duration_target_mask": False,
"latest_instruction_text": "",
"latest_instruction_target_mask": False,
}
model = FakeModel()
trainer._forward_stream_batch(
model,
["source-a"],
[step],
SimpleNamespace(),
FixedSampler(),
)
torch.testing.assert_close(
model.future_progress[0, :15],
future_progress[:15],
)
expected_tail = (
torch.tensor([1.0, 0.0, 1.0, 0.0, 1.0]).expand(15, -1)
if semantics_version in {19, 20}
else future_progress[15:]
)
torch.testing.assert_close(model.future_progress[0, 15:], expected_tail)
@pytest.mark.skip(
reason=(
"Legacy cached-hidden optimizer test; HFDv2 native-Qwen accumulation "
"is covered by test_hfdv2_qwen_streaming.py"
)
)
def test_training_step_calls_one_parallel_forward_per_stream_position():
config = FullDuplexPsi0ModelConfig(
streaming_qwen_enabled=False,
action_dim=3,
action_chunk_size=30,
action_exec_horizon=20,
observation_horizon=1,
odim=3,
n_conditions=0,
hidden_dim=48,
num_blocks=1,
dim_feedforward=64,
nhead=4,
view_feature_dim=8,
use_future_progress_embedding=False,
rtc=True,
max_delay=11,
streaming_memory_dim=16,
streaming_memory_layers=1,
streaming_memory_heads=4,
streaming_max_cache_tokens=8,
)
model = FullDuplexPsi0Model(config, vlm_model=None)
model.enable_full_source_gradient_checkpointing()
class FakeAccelerator:
num_processes = 1
sync_gradients = True
@staticmethod
def accumulate(model):
del model
return contextlib.nullcontext()
@staticmethod
def no_sync(model):
del model
return contextlib.nullcontext()
@staticmethod
def autocast():
return contextlib.nullcontext()
@staticmethod
def backward(loss):
loss.backward()
@staticmethod
def unwrap_model(model):
return model
@staticmethod
def gather(value):
return value
class FixedSampler:
@staticmethod
def sample_like(actions):
return SimpleNamespace(
sigmas=torch.full((actions.shape[0],), 0.5),
noise=torch.zeros_like(actions),
)
class Scheduler:
def __init__(self):
self.step_calls = 0
def step(self):
self.step_calls += 1
def get_last_lr(self):
return [1e-4]
trainer = PosttrainFullDuplexTrainer.__new__(PosttrainFullDuplexTrainer)
trainer.device = torch.device("cpu")
trainer.cfg = SimpleNamespace(
train=SimpleNamespace(
tbptt_unroll_steps=0,
control_loss_weight=1.0,
data_parallel="ddp",
gradient_accumulation_steps=1,
skip_resumed_steps=False,
)
)
trainer.model = model
trainer.accelerator = FakeAccelerator()
trainer.flow_random_sampler = FixedSampler()
trainer.rtc_train_overlap = 10
trainer.train_diffusion_steps = 1_000
trainer.loss_w = torch.ones(3)
trainer.loss_group_w = torch.ones(3)
class CountingSgd(torch.optim.SGD):
def __init__(self, parameters):
super().__init__(parameters, lr=1e-3)
self.step_calls = 0
def step(self, closure=None):
self.step_calls += 1
return super().step(closure)
trainer.optimizer = CountingSgd(model.parameters())
trainer.lr_scheduler = Scheduler()
def make_step(source_id: str, phase: str, value: float):
labels = {
"idle": IDLE_TOKEN_ID,
"listen": LISTEN_TOKEN_ID,
"motion": MOTION_TOKEN_ID,
}
return {
"source_id": source_id,
"vlm_hidden_states": torch.full((5, 8), value),
"attention_mask": torch.ones(5, dtype=torch.long),
"actions": torch.full((30, 3), value),
"actions_mask": torch.ones(30, 3),
"states": torch.zeros(1, 3),
"progress": torch.zeros(5),
"future_progress": torch.zeros(30, 5),
"update_semantic_cache": True,
"control_label": labels[phase],
"phase": phase,
}
batch = {
"sources": [
{
"source_id": "source-a",
"idle_steps": 1,
"listen_steps": 1,
"motion_steps": 1,
"steps": [
make_step("source-a", "idle", 0.5),
make_step("source-a", "listen", 1.0),
make_step("source-a", "motion", 2.0),
],
},
{
"source_id": "source-b",
"idle_steps": 0,
"listen_steps": 2,
"motion_steps": 1,
"steps": [
make_step("source-b", "listen", 3.0),
make_step("source-b", "motion", 4.0),
make_step("source-b", "listen", 5.0),
],
},
]
}
seen_batch_sizes: list[int] = []
seen_cache_lengths: list[tuple[int, ...]] = []
def record_batch_size(module, args, kwargs):
del module, args
seen_batch_sizes.append(int(kwargs["vlm_hidden_states"].shape[0]))
original_forward_stream_batch = trainer._forward_stream_batch
def record_cache_lengths(model, source_ids, steps, cache, sampler):
seen_cache_lengths.append(cache.sequence_lengths)
return original_forward_stream_batch(
model, source_ids, steps, cache, sampler
)
trainer._forward_stream_batch = record_cache_lengths
hook = model.register_forward_pre_hook(record_batch_size, with_kwargs=True)
try:
events = list(
trainer.iter_optimizer_steps(
batch,
global_step_start=0,
local_step=0,
)
)
finally:
hook.remove()
# Three parallel time positions, not five serial source forwards.
assert seen_batch_sizes == [2, 2, 2]
assert seen_cache_lengths == [(0, 0), (1, 1), (2, 2)]
assert len(events) == 1
assert trainer.optimizer.step_calls == 1
assert trainer.lr_scheduler.step_calls == 1
sync, metrics, source_group_end = events[0]
assert sync is True
assert source_group_end is True
assert metrics["source_step_seconds"] > 0.0
assert metrics["active_windows_per_second"] > 0.0
assert np.isfinite(metrics["loss"])
assert metrics["loss"] == pytest.approx(
metrics["loss_action"]
+ trainer.train_cfg.control_loss_weight * metrics["loss_control"]
)
assert {
"loss_objective",
"loss_window_mean",
"sources_per_second",
"optimizer_source_windows",
"parallel_source_batch",
"peak_memory_allocated_gib",
"peak_memory_reserved_gib",
"idle_steps",
"listen_steps",
"motion_steps",
"full_source_bptt",
"source_group_complete",
"chunks_in_source_group",
"chunk_index",
"optimizer_chunk_steps",
}.isdisjoint(metrics)
# A max-step stop never starts and truncates a source graph.
seen_cache_lengths.clear()
stopped_events = list(
trainer.iter_optimizer_steps(
batch,
global_step_start=1,
local_step=1,
max_optimizer_events=0,
)
)
assert stopped_events == []
assert trainer.optimizer.step_calls == 1
assert trainer.lr_scheduler.step_calls == 1
assert seen_cache_lengths == []
assert trainer._last_cache_state == "empty_source_boundary"
def test_fully_padded_rank_still_advances_one_flow_draw():
trainer = PosttrainFullDuplexTrainer.__new__(PosttrainFullDuplexTrainer)
trainer.device = torch.device("cpu")
class CountingSampler:
def __init__(self):
self.shapes = []
def sample_like(self, actions):
self.shapes.append(tuple(actions.shape))
return None
trainer.flow_random_sampler = CountingSampler()
trainer._advance_padded_flow_rng(
SimpleNamespace(action_exec_horizon=20, action_dim=3),
source_batch_size=2,
)
assert trainer.flow_random_sampler.shapes == [(2, 30, 3)]
def test_cache_free_checkpoint_marker_records_window_step_training(
tmp_path,
monkeypatch,
):
contract = {
"format": "hfd_qwen_serial_train_infer_contract_v6",
"identity": "test",
}
data_contract = {
"format": "hfd_continuous_office_production_v1",
"source_count": 2,
"mixed_window_count": 9,
"background_source_count": 1,
"background_window_count": 20,
"initial_idle_max_steps": 5,
"stationary_blend_frames": 20,
"safety_steps": 10,
"assignment_seed": 260730,
"assignment_sha256": "assignment",
}
monkeypatch.setattr(
"psi.trainers.posttrain_full_duplex."
"_active_continuous_data_contract_identity",
lambda: data_contract,
)
trainer = PosttrainFullDuplexTrainer.__new__(PosttrainFullDuplexTrainer)
trainer.cfg = SimpleNamespace(
train=SimpleNamespace(
full_duplex_semantics_version=11,
joint_velocity_loss_weight=0.10,
joint_acceleration_loss_weight=0.05,
root_continuity_loss_weight=0.10,
tbptt_unroll_steps=0,
full_source_gradient_checkpointing=True,
qwen_kv_gradient_horizon_windows=1,
source_accumulation_groups=1,
train_batch_size=8,
),
model=SimpleNamespace(
streaming_qwen_trainable_last_layers=2,
streaming_qwen_train_final_norm=True,
control_condition_train_mode="hard",
control_condition_inference_mode="hard",
),
)
trainer._hfdv2_checkpoint_contract = lambda: contract
class FakeAccelerator:
is_main_process = True
@staticmethod
def wait_for_everyone():
return None
trainer.accelerator = FakeAccelerator()
trainer._last_cache_state = "empty_source_boundary"
trainer._last_source_group_complete = True
trainer._write_full_duplex_boundary_marker(
str(tmp_path),
global_step=7,
)
marker = json.loads(
(tmp_path / "full_duplex_source_boundary.json").read_text()
)
assert marker["global_step"] == 7
assert marker["full_duplex_semantics_version"] == 11
assert marker["architecture"] == "hfd_qwen_native_kv_serial_control"
assert marker["online_raw_audio"] is True
assert marker["semantic_memory"] == "qwen_dynamic_cache"
assert marker["action_streaming_memory"] is False
assert marker["serial_control_conditioning"] is True
assert marker["control_condition_train_mode"] == "hard"
assert marker["control_condition_inference_mode"] == "hard"
assert marker["control_condition_source"] == "predicted_no_ground_truth"
assert marker["joint_velocity_loss_weight"] == 0.10
assert marker["joint_acceleration_loss_weight"] == 0.05
assert marker["root_continuity_loss_weight"] == 0.10
assert marker["optimizer_scope"] == "one_aligned_stream_window"
assert marker["optimizer_chunk_steps"] == 1
assert marker["source_tail_loss_normalization"] == (
"fixed_global_source_capacity"
)
assert marker["source_slot_policy"] == (
"qwen_boundary_dynamic_refill_v1"
)
assert marker["qwen_kv_gradient_horizon_windows"] == 1
assert marker["source_accumulation_groups"] == 1
assert marker["effective_sources_per_gpu"] == 8
assert marker["action_gradient_checkpointing"] is True
assert marker["cache_state"] == "empty_source_boundary"
assert marker["source_group_complete"] is True
assert marker["train_infer_contract"] == contract
assert marker["continuous_audio_data_contract"] == data_contract
def test_small_full_duplex_model_has_finite_action_and_control_gradients():
config = FullDuplexPsi0ModelConfig(
streaming_qwen_enabled=False,
action_dim=3,
action_chunk_size=30,
action_exec_horizon=20,
observation_horizon=1,
odim=3,
n_conditions=0,
hidden_dim=48,
num_blocks=1,
dim_feedforward=64,
nhead=4,
view_feature_dim=8,
use_future_progress_embedding=False,
rtc=True,
max_delay=11,
streaming_memory_dim=16,
streaming_memory_layers=1,
streaming_memory_heads=4,
streaming_max_cache_tokens=8,
)
model = FullDuplexPsi0Model(config, vlm_model=None)
cache = model.begin_source(["source-a"])
output = model(
source_ids=["source-a"],
kv_cache=cache,
update_semantic_cache=True,
vlm_hidden_states=torch.randn(1, 5, 8),
attention_mask=torch.ones(1, 5, dtype=torch.long),
action_samples=torch.randn(1, 20, 3),
known_action_prefix=torch.randn(1, 10, 3),
states=torch.randn(1, 1, 3),
timestep=torch.tensor([500.0]),
)
assert output.action.shape == (1, 20, 3)
assert output.control_logits.shape == (1, 3)
loss = output.action.float().square().mean() + F.cross_entropy(
output.control_logits.float(), torch.tensor([LISTEN_TOKEN_ID])
)
assert torch.isfinite(loss)
loss.backward()
assert model.control_head[-1].weight.grad is not None
assert torch.isfinite(model.control_head[-1].weight.grad).all()
@pytest.mark.parametrize("action_recompute", [False, True])
def test_late_window_loss_reaches_early_source_input_through_kv_graph(
action_recompute,
):
config = FullDuplexPsi0ModelConfig(
streaming_qwen_enabled=False,
action_dim=3,
action_chunk_size=30,
action_exec_horizon=20,
observation_horizon=1,
odim=3,
n_conditions=0,
hidden_dim=48,
num_blocks=1,
dim_feedforward=64,
nhead=4,
view_feature_dim=8,
use_future_progress_embedding=False,
rtc=True,
max_delay=11,
streaming_memory_dim=16,
streaming_memory_layers=1,
streaming_memory_heads=4,
streaming_max_cache_tokens=8,
)
model = FullDuplexPsi0Model(config, vlm_model=None)
if action_recompute:
model.enable_full_source_gradient_checkpointing()
cache = model.begin_source(["source-a"])
early_hidden = torch.randn(1, 5, 8, requires_grad=True)
early = model(
source_ids=["source-a"],
kv_cache=cache,
update_semantic_cache=True,
vlm_hidden_states=early_hidden,
attention_mask=torch.ones(1, 5, dtype=torch.long),
action_samples=torch.randn(1, 20, 3),
known_action_prefix=torch.randn(1, 10, 3),
states=torch.randn(1, 1, 3),
timestep=torch.tensor([500.0]),
)
late = model(
source_ids=["source-a"],
kv_cache=early.kv_cache,
update_semantic_cache=True,
vlm_hidden_states=torch.randn(1, 5, 8),
attention_mask=torch.ones(1, 5, dtype=torch.long),
action_samples=torch.randn(1, 20, 3),
known_action_prefix=torch.randn(1, 10, 3),
states=torch.randn(1, 1, 3),
timestep=torch.tensor([500.0]),
)
late_loss = late.action.float().square().mean() + F.cross_entropy(
late.control_logits.float(),
torch.tensor([MOTION_TOKEN_ID]),
)
late_loss.backward()
assert early_hidden.grad is not None
assert torch.isfinite(early_hidden.grad).all()
assert float(early_hidden.grad.abs().sum()) > 0.0
def test_stream_condition_is_encoded_once_across_flow_solver_steps():
config = FullDuplexPsi0ModelConfig(
streaming_qwen_enabled=False,
action_dim=3,
action_chunk_size=30,
action_exec_horizon=20,
observation_horizon=1,
odim=3,
n_conditions=0,
hidden_dim=48,
num_blocks=1,
dim_feedforward=64,
nhead=4,
view_feature_dim=8,
use_future_progress_embedding=False,
rtc=True,
max_delay=11,
streaming_memory_dim=16,
streaming_memory_layers=1,
streaming_memory_heads=4,
streaming_max_cache_tokens=8,
)
model = FullDuplexPsi0Model(config, vlm_model=None).eval()
cache = model.begin_source(["source-a"])
speech = model.encode_stream_input(
source_ids=["source-a"],
kv_cache=cache,
update_semantic_cache=True,
vlm_hidden_states=torch.randn(1, 5, 8),
attention_mask=torch.ones(1, 5, dtype=torch.long),
)
assert speech.kv_cache.sequence_length == 1
for timestep in (900.0, 100.0):
action = model.forward_action_conditioned(
condition=speech,
action_samples=torch.randn(1, 20, 3),
known_action_prefix=torch.randn(1, 10, 3),
states=torch.randn(1, 1, 3),
timestep=torch.tensor([timestep]),
)
assert action.shape == (1, 20, 3)
assert speech.kv_cache.sequence_length == 1
padded = model.encode_stream_input(
source_ids=["source-a"],
kv_cache=speech.kv_cache,
update_semantic_cache=False,
vlm_hidden_states=torch.randn(1, 4, 8),
attention_mask=torch.ones(1, 4, dtype=torch.long),
)
assert padded.kv_cache.sequence_length == 1
def test_length_group_sampler_balances_each_global_rank_group():
class Dataset:
lengths = [100, 1, 98, 2, 96, 3, 94, 4]
def __len__(self):
return len(self.lengths)
def estimated_stream_steps(self, index):
return self.lengths[index]
dataset = Dataset()
sampler = SourceLengthGroupedSampler(
dataset, world_size=2, batch_size=1, seed=17
)
assert len(sampler) // sampler.group_size == 4
first = list(sampler)
sampler.set_epoch(1)
second = list(sampler)
assert sorted(first) == list(range(len(dataset)))
assert sorted(second) == list(range(len(dataset)))
assert first != second
for offset in range(0, len(first), 2):
pair = [dataset.lengths[index] for index in first[offset : offset + 2]]
assert max(pair) - min(pair) <= 3
def test_length_group_sampler_balances_long_source_work_across_ranks():
class Dataset:
lengths = [1_000, 900, 800, 700, 600, 500, 400, 300]
def __len__(self):
return len(self.lengths)
def estimated_stream_steps(self, index):
return self.lengths[index]
dataset = Dataset()
sampler = SourceLengthGroupedSampler(
dataset, world_size=2, batch_size=4, seed=23
)
order = list(sampler)
rank_rows = [order[rank::2] for rank in range(2)]
assert all(len(rows) == 4 for rows in rank_rows)
assert {
max(dataset.lengths[index] for index in rows)
for rows in rank_rows
} == {900, 1_000}
for offset in range(0, len(order), 2):
aligned = [
dataset.lengths[index]
for index in order[offset : offset + 2]
]
assert max(aligned) - min(aligned) <= 100
def test_source_batching_loader_groups_independently_loaded_sources():
singleton_loader = [
{"sources": [{"source_id": f"source-{index}"}]}
for index in range(6)
]
loader = SourceBatchingLoader(singleton_loader, batch_size=3)
assert len(loader) == 2
batches = list(loader)
assert [
[source["source_id"] for source in batch["sources"]]
for batch in batches
] == [
["source-0", "source-1", "source-2"],
["source-3", "source-4", "source-5"],
]
def test_source_batching_loader_rebuckets_materialized_exact_lengths():
lengths = [8, 1, 7, 2]
singleton_loader = [
{
"sources": [
{
"source_id": f"source-{index}",
"steps": [{} for _ in range(length)],
}
]
}
for index, length in enumerate(lengths)
]
loader = SourceBatchingLoader(
singleton_loader,
batch_size=2,
length_lookahead_batches=2,
)
batches = list(loader)
assert [
[len(source["steps"]) for source in batch["sources"]]
for batch in batches
] == [[1, 2], [7, 8]]
assert sorted(
source["source_id"]
for batch in batches
for source in batch["sources"]
) == [f"source-{index}" for index in range(4)]
def test_source_batching_loader_flushes_short_final_lookahead_block():
singleton_loader = [
{
"sources": [
{
"source_id": f"source-{index}",
"steps": [{} for _ in range(length)],
}
]
}
for index, length in enumerate([6, 1, 5, 2, 4, 3])
]
loader = SourceBatchingLoader(
singleton_loader,
batch_size=2,
length_lookahead_batches=2,
)
assert [
[len(source["steps"]) for source in batch["sources"]]
for batch in loader
] == [[1, 2], [5, 6], [3, 4]]
def test_source_prefetch_defaults_to_two_complete_local_batches(monkeypatch):
monkeypatch.delenv("PSI_DATALOADER_PREFETCH_FACTOR", raising=False)
assert _source_prefetch_factor(workers=16, local_source_batch=64) == 8
monkeypatch.setenv("PSI_DATALOADER_PREFETCH_FACTOR", "3")
assert _source_prefetch_factor(workers=16, local_source_batch=64) == 3
def test_source_prefetch_keeps_two_batches_ready_for_exact_lookahead(monkeypatch):
monkeypatch.delenv("PSI_DATALOADER_PREFETCH_FACTOR", raising=False)
assert (
_source_prefetch_factor(
workers=16,
local_source_batch=128,
)
== 16
)
def test_session_voice_lookup_index_preserves_exact_audio_source():
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
voices = (
AudioSource("seed_audio/000001/female_01_en", ()),
AudioSource("seed_audio/000001/female_01_zh", ()),
)
dataset.audio_catalog = {"seed_audio/000001": voices}
dataset._audio_catalog_by_voice_name = {
"seed_audio/000001": {
dataset._voice_name(voice): voice for voice in voices
}
}
record = {
"row_id": 1,
"audio_dir": "seed_audio/000001",
"_session_voice_name": "female_01_zh",
}
assert dataset._voice_for_record(record) is voices[1]
record["_exclude_zh_voices"] = True
with pytest.raises(RuntimeError, match="unavailable or ambiguous"):
dataset._voice_for_record(record)
def test_motion_cache_is_reserved_for_reused_stationary_sources(
monkeypatch, tmp_path
):
dataset = FullDuplexAudioCommandDataset.__new__(
FullDuplexAudioCommandDataset
)
stationary = tmp_path / "stationary"
task = tmp_path / "task"
dataset.motion_cache_size = 256
dataset._motion_cache = OrderedDict()
dataset._stationary_motion_paths = frozenset({str(stationary)})
reads = {str(stationary): 0, str(task): 0}
class FakeFrame:
def __init__(self, values):
self.values = values
def to_numpy(self, dtype):
return self.values.astype(dtype)
def fake_read_csv(path):
motion_dir = str(Path(path).parent)
reads[motion_dir] += 1
columns = {
"joint_pos.csv": 29,
"body_pos.csv": 3,
"body_quat.csv": 4,
}[Path(path).name]
return FakeFrame(np.zeros((2, columns), dtype=np.float32))
monkeypatch.setattr(
"psi.data.seed.full_duplex_audio_command_dataset.pd.read_csv",
fake_read_csv,
)
dataset._load_motion(stationary)
dataset._load_motion(stationary)
dataset._load_motion(task)
dataset._load_motion(task)
assert reads[str(stationary)] == 3
assert reads[str(task)] == 6
assert list(dataset._motion_cache) == [str(stationary)]
def test_manual_gradient_average_includes_remote_missing_grad(monkeypatch):
first = torch.nn.Parameter(torch.zeros(2))
second = torch.nn.Parameter(torch.zeros(1))
first.grad = torch.tensor([2.0, 4.0])
second.grad = None
monkeypatch.setattr(torch.distributed, "is_initialized", lambda: True)
def fake_all_reduce(flat, op):
del op
flat.add_(torch.tensor([4.0, 8.0, 6.0]))
monkeypatch.setattr(torch.distributed, "all_reduce", fake_all_reduce)
average_full_duplex_gradients([first, second], world_size=2, bucket_cap_mb=1)
torch.testing.assert_close(first.grad, torch.tensor([3.0, 6.0]))
torch.testing.assert_close(second.grad, torch.tensor([3.0]))
def test_audio_windows_and_motion_start_use_integer_sample_timeline():
assert complete_audio_clip_starts(9_599) == []
assert complete_audio_clip_starts(16_100) == [0, 6_400]
assert aligned_motion_start_frame(16_000) == 50
assert aligned_motion_start_frame(16_100) == 51
|