Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add precalculated values of sin/cos for speeding up FFT #1142

Merged

Conversation

AlexandrGraschenkov
Copy link
Contributor

@AlexandrGraschenkov AlexandrGraschenkov commented Jul 30, 2023

The code works in the same way as before, except that we don't spend time on sin/cos calculations. This is a real bottleneck for FFT calculations.

System info:

Apple M1 Pro, Mac OS Ventura 13.3
n_threads = 4 / 10 | AVX = 0 | AVX2 = 0 | AVX512 = 0 | FMA = 0 | NEON = 1 | ARM_FMA = 1 | F16C = 0 | FP16_VA = 1 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 0 | VSX = 0 | COREML = 0 | OPENVINO = 0 |

Testing base.en model on: ./main -f samples/a13.wav

Version mel time
Original 55.17 ms
Cos/sin table 19.15 ms

Testing base.en model on: ./main -f samples/diffusion2023-07-03.wav

Version mel time
Original 2142.07 ms
Cos/sin table 734.42 ms

@bobqianic
Copy link
Collaborator

@AlexandrGraschenkov If you have some free time, could you do me a favor and test how much of an improvement there would be if the optimization in #1148 is combined with your optimization? This is because I've set it to skip FFT calculations and output the results directly when the FFT input is all zeros during the calculation of log Mel. If you have more time, please help me see how much the quality of speech-to-text has improved. Thank you!

@ggerganov
Copy link
Owner

This is because I've set it to skip FFT calculations and output the results directly when the FFT input is all zeros during the calculation of log Mel.

How often do you observe this to be the case? I would guess it never happens for normal audio

@bobqianic
Copy link
Collaborator

This is because I've set it to skip FFT calculations and output the results directly when the FFT input is all zeros during the calculation of log Mel.

How often do you observe this to be the case? I would guess it never happens for normal audio

After the log Mel correction, we should have 30 seconds of zeros, equating to 480,000 samples or approximately 2,998 frames. The presence of these zeros is due to the zero padding added to the end of the sample before performing calculations. With each FFT taking an estimated 25 microseconds, we anticipate a time saving of 75ms. Additionally, by removing the if statement from the loop, we can reduce the number of branch predictions.

@ggerganov
Copy link
Owner

Ok, makes sense. This change won't interfere with your changes and it is OK to merge, correct?

I'm slowly working my way through the oldest PRs and will soon reach your changes.

@bobqianic
Copy link
Collaborator

Ok, makes sense. This change won't interfere with your changes and it is OK to merge, correct?

I'm slowly working my way through the oldest PRs and will soon reach your changes.

No, in this PR we should switch from sin and cos to sinf and cosf. Once that's done, everything should be fine.

whisper.cpp Outdated Show resolved Hide resolved
whisper.cpp Outdated Show resolved Hide resolved
ggerganov and others added 2 commits August 25, 2023 15:48
Co-authored-by: bobqianic <[email protected]>
Co-authored-by: bobqianic <[email protected]>
@ggerganov ggerganov merged commit c84cf87 into ggerganov:master Aug 25, 2023
34 checks passed
jacobwu-b pushed a commit to jacobwu-b/Transcriptify-by-whisper.cpp that referenced this pull request Oct 24, 2023
…erganov#1142)

* Add sin/cos precalculated values to speedup FFT

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

---------

Co-authored-by: Georgi Gerganov <[email protected]>
Co-authored-by: bobqianic <[email protected]>
jacobwu-b pushed a commit to jacobwu-b/Transcriptify-by-whisper.cpp that referenced this pull request Oct 24, 2023
…erganov#1142)

* Add sin/cos precalculated values to speedup FFT

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

---------

Co-authored-by: Georgi Gerganov <[email protected]>
Co-authored-by: bobqianic <[email protected]>
vonstring pushed a commit to vonstring/whisper.cpp that referenced this pull request Nov 7, 2023
…erganov#1142)

* Add sin/cos precalculated values to speedup FFT

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

---------

Co-authored-by: Georgi Gerganov <[email protected]>
Co-authored-by: bobqianic <[email protected]>
landtanin pushed a commit to landtanin/whisper.cpp that referenced this pull request Dec 16, 2023
…erganov#1142)

* Add sin/cos precalculated values to speedup FFT

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

---------

Co-authored-by: Georgi Gerganov <[email protected]>
Co-authored-by: bobqianic <[email protected]>
iThalay pushed a commit to iThalay/whisper.cpp that referenced this pull request Sep 23, 2024
…erganov#1142)

* Add sin/cos precalculated values to speedup FFT

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

* Update whisper.cpp

Co-authored-by: bobqianic <[email protected]>

---------

Co-authored-by: Georgi Gerganov <[email protected]>
Co-authored-by: bobqianic <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants