Live data from Hacker News

Show HN: Port of OpenAI's Whisper model in C/C++

github.com

1–10 of 93 posts

Show HN: Port of OpenAI's Whisper model in C/C++

#1
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

Show HN: Port of OpenAI's Whisper model in C/C++
github.com

Re: Show HN: Port of OpenAI's Whisper model in C/C++

#3
10/10 you're doing god's work my friend, can't wait to spend some time this weekend to try and understand what's going on here. I can't overstate how much I value small libraries. I can't think of a faster way to learn about a concept than to step through someone else's barebones implementation.

Re: Show HN: Port of OpenAI's Whisper model in C/C++

#4

10/10 you're doing god's work my friend, can't wait to spend some time this weekend to try and understand what's going on here. I can't overstate how much I value small libraries. I can't think of a faster way to learn about a concept than to step through someone else's barebones implementation.

Thanks! Indeed, I agree that the project has an educational aspect and value. For me, it helped me get a better understanding of the neural network layers involved in the transformer model. Also, it was a good playground to practice my low-level optimization techniques. I guess another cool thing was that with the help of the community, we came up with a faster way to evaluate the Encoder (at the cost of some accuracy), which ultimately enabled the WASM and RPi4 examples (see #137 if interested in the discussion).

Re: Show HN: Port of OpenAI's Whisper model in C/C++

#8
post #6

Awesome work. Curious whether the whisper model could be ported to tinygrad and how the performance would compare to your implementation.

Thanks! I'm also interested in seeing a CPU comparison against tinygrad. From what I've seen, tinygrad already utilizes the AMX coprocessor, so I expect to have comparable performance between the 2 on Apple Silicon.

Re: Show HN: Port of OpenAI's Whisper model in C/C++

#9
Offline models, especially speech recognition, is a game changer for many apps.

A fully CPU based implementation, simple enough with minimal dependencies is also something that helps tremendously reduce the initial friction and enable potential low-cost applications.

Excellent and impressive work, can’t wait to try this thing at home.

Re: Show HN: Port of OpenAI's Whisper model in C/C++

#10
> about 2-3 times faster compared to the current PyTorch implementation

This is surprising to me. Is this about CPU only? Then it would make sense.

Also, is there a particular reason why the whole code is basically 2 massive files (3k and 8k lines respectively)?

Post reply on HN