Spaces:
Sleeping
Sleeping
goldwaving
commited on
Remove unnecessary memory reallocation in fft (#2080)
Browse filesfft_out needs to be twice the frame_size, not the frame_step. It is resized in fft() anyway, but this change prevents an unnecessary reallocation.
n_fft must match the mel filter size, so it is best not to calculate it from the framesize.
We only need to get the magnitudes for half the spectrum since the other half is a mirror and not used in the mel filter loop later.
- whisper.cpp +6 -4
whisper.cpp
CHANGED
|
@@ -2900,11 +2900,13 @@ static void log_mel_spectrogram_worker_thread(int ith, const std::vector<float>
|
|
| 2900 |
int n_samples, int frame_size, int frame_step, int n_threads,
|
| 2901 |
const whisper_filters & filters, whisper_mel & mel) {
|
| 2902 |
std::vector<float> fft_in(frame_size, 0.0);
|
| 2903 |
-
std::vector<float> fft_out(2 *
|
| 2904 |
-
|
| 2905 |
-
int n_fft = 1 + (frame_size / 2);
|
| 2906 |
int i = ith;
|
| 2907 |
|
|
|
|
|
|
|
|
|
|
| 2908 |
// calculate FFT only when fft_in are not all zero
|
| 2909 |
for (; i < std::min(n_samples / frame_step + 1, mel.n_len); i += n_threads) {
|
| 2910 |
const int offset = i * frame_step;
|
|
@@ -2923,7 +2925,7 @@ static void log_mel_spectrogram_worker_thread(int ith, const std::vector<float>
|
|
| 2923 |
|
| 2924 |
// Calculate modulus^2 of complex numbers
|
| 2925 |
// Use pow(fft_out[2 * j + 0], 2) + pow(fft_out[2 * j + 1], 2) causes inference quality problem? Interesting.
|
| 2926 |
-
for (int j = 0; j <
|
| 2927 |
fft_out[j] = (fft_out[2 * j + 0] * fft_out[2 * j + 0] + fft_out[2 * j + 1] * fft_out[2 * j + 1]);
|
| 2928 |
}
|
| 2929 |
|
|
|
|
| 2900 |
int n_samples, int frame_size, int frame_step, int n_threads,
|
| 2901 |
const whisper_filters & filters, whisper_mel & mel) {
|
| 2902 |
std::vector<float> fft_in(frame_size, 0.0);
|
| 2903 |
+
std::vector<float> fft_out(2 * frame_size);
|
| 2904 |
+
int n_fft = filters.n_fft;
|
|
|
|
| 2905 |
int i = ith;
|
| 2906 |
|
| 2907 |
+
// make sure n_fft == 1 + (WHISPER_N_FFT / 2), bin_0 to bin_nyquist
|
| 2908 |
+
assert( n_fft == 1 + (frame_size / 2) );
|
| 2909 |
+
|
| 2910 |
// calculate FFT only when fft_in are not all zero
|
| 2911 |
for (; i < std::min(n_samples / frame_step + 1, mel.n_len); i += n_threads) {
|
| 2912 |
const int offset = i * frame_step;
|
|
|
|
| 2925 |
|
| 2926 |
// Calculate modulus^2 of complex numbers
|
| 2927 |
// Use pow(fft_out[2 * j + 0], 2) + pow(fft_out[2 * j + 1], 2) causes inference quality problem? Interesting.
|
| 2928 |
+
for (int j = 0; j < n_fft; j++) {
|
| 2929 |
fft_out[j] = (fft_out[2 * j + 0] * fft_out[2 * j + 0] + fft_out[2 * j + 1] * fft_out[2 * j + 1]);
|
| 2930 |
}
|
| 2931 |
|