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
moloch-hai3 replies#
There is no language called "C/C++".

If, to use it, you need C++, it's a C++ system, period. If parts are in C, that is an implementation detail, and is furthermore pointless; modernizing those to C++ would make them more robust and probably faster. Using C anywhere just makes it as bug-prone in those parts as C code everywhere is.

MaxBarraclough1 reply#
> If, to use it, you need C++, it's a C++ system, period. If parts are in C, that is an implementation detail

C and C++ are different languages, you can't necessarily treat a codebase of mixed C and C++ as if it were written entirely in C++.
This isn't purely academic, either. For a long time the MSVC compiler had pretty good C++ language support but surprisingly poor C support, which inconvenienced some folks.
> modernizing those to C++ would make them more robust and probably faster
I agree that adopting C++'s insistence on explicit type conversions is a good move for C codebases. I'd be surprised if performance were improved by reworking a C codebase to get it to compile as C++, though.

moloch-hai#
You can, in fact, compile the C code with a C++ compiler, given trivial adjustments. Adopting "explicit type conversions" would have similarly trivial results. The benefits would come from leaning into higher-level organization.

C++ code going faster than the C is routine. It doesn't come without activity, but the activity yields a more maintainable system, so is commonly done.
MSVC's C compiler only recognized C90. It was generally easier to make your C99 code compile as C++ than to backport it to C90.

kgwgk1 reply#
> There is no language called "C/C++".

He clearly means that he used two languages: C and C++.
"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++."

moloch-hai1 reply#
It is obvious what he did just from reading what he wrote, after. It is not clear that he meant anything at all by the expression. You cannot use his thing in any but a C++ program, so mentioning C at all only adds confusion.
kgwgk#
It is clear what he meant by the distiction:

  implemented a minimalistic tensor library in C

  ported the high-level architecture of the model in C++
The C part - which accounts for most of the lines of code - may be of interest to someone who wants to use it as the foundation for implementing a different model - not necessarily in C++
mh71 reply#
>There is no language called "C/C++".

Yes there is.
Take the union of C and C++ which is valid syntax (and same semantics) in both languages, voila; C/C++.

moloch-hai#
But it is not, in fact, written in such a "language". Nothing is.
xchkr13372 replies#
Why not Rust? C++ is obsolete
ducktective1 reply#
I wonder how ChatGPT3 would respond to this?

If they crawled /r/programming and hackernews, it should be annoyed and come up with something along the lines of "fork it and write in in Rust yourself", "C++ has lots of inertia", "his main stack is C/C++", etc

allisdust#
While there is no pure rust model yet, there is a crate with bindings to the cpp model (probably not to this one).
stephc_int13#
This kind of comment is exactly what Rust skeptics are talking about when they are referring to the toxic Rust community.

All comments shown.