| 1866 | } |
| 1867 | |
| 1868 | VARP Talker::ditForwardAsync(const int codec_size, const int* codec_tokens, const float* initial_noise) { |
| 1869 | auto code = _Const(codec_tokens, {1, codec_size}, NCHW, halide_type_of<int>()); |
| 1870 | const int max_duration = codec_size * 2; |
| 1871 | auto outputs = mPreDit_async->onForward({mCond_async, mSpk_async, code}); |
| 1872 | auto code_embeds = outputs[0]; |
| 1873 | auto rope = outputs[1]; |
| 1874 | auto mask = outputs[2]; |
| 1875 | const int steps = mConfig->dit_steps(); |
| 1876 | const int solver = mConfig->dit_solver(); |
| 1877 | const float step_ratio = 1.0 / (steps - 1); |
| 1878 | auto forward_dit = [&](float t, Express::VARP x) { |
| 1879 | return mDit_async->onForward({x, code_embeds, rope, mask, _Const(t, {1}, NCHW)})[0]; |
| 1880 | }; |
| 1881 | auto y0 = _Input({1, max_duration, 80}, NCHW, halide_type_of<float>()); |
| 1882 | if (initial_noise) { |
| 1883 | for (int i = 0; i < max_duration * 80; ++i) { |
| 1884 | y0->writeMap<float>()[i] = initial_noise[i]; |
| 1885 | } |
| 1886 | } else { |
| 1887 | std::random_device rd; |
| 1888 | std::mt19937 generator(rd()); |
| 1889 | std::normal_distribution<double> distribution(0.0, 1.0); |
| 1890 | for (int i = 0; i < max_duration * 80; ++i) { |
| 1891 | y0->writeMap<float>()[i] = distribution(generator); |
| 1892 | } |
| 1893 | } |
| 1894 | for (int i = 0; i < steps - 1; i++) { |
| 1895 | float t0 = 1 - std::cos(M_PI / 2 * i * step_ratio); |
| 1896 | float t1 = 1 - std::cos(M_PI / 2 * (i + 1) * step_ratio); |
| 1897 | float dt = t1 - t0; |
| 1898 | auto k1 = mDit_async->onForward({y0, code_embeds, rope, mask, _Const(t0, {1}, NCHW)})[0]; |
| 1899 | if (solver == 1) { |
| 1900 | y0 = y0 + k1 * _Scalar<float>(dt); |
| 1901 | } else { |
| 1902 | constexpr float one_third = 1.0 / 3.0; |
| 1903 | constexpr float two_third = 2.0 / 3.0; |
| 1904 | auto kk1 = _Clone(k1, true); |
| 1905 | auto k2 = forward_dit(t0 + dt * one_third, y0 + k1 * _Scalar<float>(dt * one_third)); |
| 1906 | auto kk2 = _Clone(k2, true); |
| 1907 | auto k3 = forward_dit(t0 + dt * two_third, y0 + _Scalar<float>(dt) * (k2 - k1 * _Scalar<float>(two_third))); |
| 1908 | auto kk3 = _Clone(k3, true); |
| 1909 | auto k4 = forward_dit(t1, y0 + _Scalar<float>(dt) * (k1 - k2 + k3)); |
| 1910 | auto kk4 = _Clone(k4, true); |
| 1911 | auto dy = (kk1 + _Scalar<float>(3.0) * (kk2 + kk3) + kk4) * _Scalar<float>(dt * 0.125); |
| 1912 | y0 = y0 + dy; |
| 1913 | } |
| 1914 | } |
| 1915 | return _Permute(y0, {0, 2, 1}); |
| 1916 | } |
| 1917 | |
| 1918 | VARP Talker::bigvganForwardAsync(VARP mel) { |
| 1919 | return mBigvgan_async->forward(mel); |