Lurker News
Hi HN,

OpenAI recently released a model for automatic speech recognition called Whisper [0]. I decided to reimplement the inference of the model from scratch using C/C++. To achieve this I implemented a minimalistic tensor library in C and ported the high-level architecture of the model in C++. The entire code is less than 8000 lines of code and is contained in just 2 source files without any third-party dependencies. The Github project is here:
https://github.com/ggerganov/whisper.cpp
With this implementation I can very easily build and run the model - “make base.en”. It also allows me to run it on a wide range of devices. For example, I have provided examples of running the model on an iPhone, Raspberry Pi 4 and even in a web page via WebAssembly!
The implementation runs fully on the CPU and utilizes FP16, AVX intrinsics on x86 architectures and NEON + Accelerate framework on Apple Silicon. The latter is especially efficient and I observe that the inference is about 2-3 times faster compared to the current PyTorch implementation provided by OpenAI when running it on my MacBook M1 Pro. The WASM port utilizes SIMD 128-bit intrinsics - a feature supported in some modern web browsers [1].
I am very happy with the performance that I observe on Apple Silicon devices. I didn’t expect that the Accelerate framework [2] (i.e. CBLAS) offers such a dramatic performance boost for matrix multiplications so I was very pleasantly surprised! To enable the framework in your C/C++ projects, all you have to do is add `-framework Accelerate` to your clang command-line flags.
This entire exercise of implementing the Whisper model was very interesting to me and helped me understand a lot about how the transformer architecture works. I also got a lot of positive feedback from people finding and using my project. We brainstormed on a lot of interesting tools that can potentially be created with this library (such as speech-to-text plugin for Vim, RPi4 voice assistant, WASM chat bot, etc). If interested, checkout the “Examples” section and the “Show and tell” discussions for some ideas!
Would love to know what you think about this project and about your experience with using the Accelerate framework in any of your projects. Cheers!
[0] https://github.com/openai/whisper
[1] https://chromestatus.com/feature/6533147810332672
[2] https://developer.apple.com/documentation/accelerate

Comments (87) 48 threads

Earlier comments
NWoodsman#
For those who want to try, here are the steps I took over about an hour to set it up:

1. Downloaded the Win10 artifact: https://github.com/ggerganov/whisper.cpp/actions/runs/363552... at the bottom of the page by logging in to Github. Extract and placed this folder in my F:\ drive renaming it to 'Whisper'.
2. Downloaded `ggml-large.bin` here: https://huggingface.co/datasets/ggerganov/whisper.cpp/tree/m.... Within F:\Whisper, add a folder named 'models'. Move ggml-large.bin to the 'models' folder.
3. Downloaded ffmpeg, extracted the archive to F:\FFMpeg, and set the environment variable by going to (right click) This PC -> Properties -> Advanced system settings -> (Advanced tab) -> Environment Variables -> click Path -> Edit -> (paste in ffmpegs path i.e. F:\FFMpeg\)
4. Use PowerShell to run ffmpeg against an mp3 file, to convert it to WAV (which is the only format that works) i.e.:
ffmpeg -i F:\Rec\input.mp3 -ar 16000 -ac 1 -c:a pcm_s16le F:\Output\output.wav
5. Open PowerShell again, `cd` to the Whisper folder, and ran this:
./main -m models/ggml-large.bin -f F:\Rec\output.wav

eigenvalue2 replies#
This is really awesome, great work! I downloaded a long video from YouTube that is very challenging to transcribe (it is an interview with Hayek, who was both very soft spoken and had a thick German accent) because I wanted to evaluate OpenAI’s claims about whisper being “superhuman” in recognition. It was a bit picky about having the audio in the exact right format (it needs to be 16kHz wav files— would be really nice it just include ffmpeg in the release and automatically pipe any input first to ffmpeg to convert to the desired format), but once it got started it just cranked away extremely quickly on my iMac M1. And the results do seem to be pretty good. I just wish the model also did some basic speaker identification, so it could insert “Speaker1:” or something at the beginning of each line/timestamp. Even if it’s not sure, it could insert “Speaker:” and that would still be useful.
ggerganov2 replies#
Btw, there is the `yt-wsp.sh` helper script to download, convert and transcribe a video by given url:

  ./examples/yt-wsp.sh
eigenvalue#
Ah thank you, I somehow missed that! Amazing job on this. You could literally create whole new companies from this system.
eigenvalue1 reply#
I actually couldn't get the script to work-- it keeps complaining that it can't find the Whisper executable. Do you know what I would have to do first to get it to work starting from a "blank slate" and cloning the repo? Thanks!
zimpenfish#
It's trying to run `WHISPER_EXECUTABLE` which it defaults to `whisper` - but if you follow the instructions in the README, you end up with `main`.

    WHISPER_EXECUTABLE=main examples/yt-wsp.sh youtube_url
Or the script itself prints out a handy hint.

    Whisper needs to be built into the main binary with make, then you can rename it to something like 'whisper' and add it to your PATH for convenience.
codetrotter#
This is very cool. Excellent work!

Perhaps in combination with https://www.npmjs.com/package/peertube-plugin-transcription your port of Whisper could be used for generating subtitles for videos in PeerTube?
I just recently set up a PeerTube instance of my own and uploaded my first video on it ("No Brain Required - ChatGPT solves Advent of Code in Rust, episode 1", https://video.nstr.no/w/6z7PxB4J92H3NHhgMmfYVw)
I want to try and make use of your port of Whisper on my PeerTube instance, so that I can have subtitles generated for my videos on it :D

fimdomeio#
I don't fully understand what's going on behind the scenes, but I tried to use the repo some days ago (guess there's even a new model now) and everything seemed very simple to build to try so thank you for your amazing work.

Load more comments