Aussie AI

C++ Compile-time Techniques

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

C++ Compile-time Techniques

Compile-time processing is the optimal way to run a program. All the work is done by the compiler and none by your program. There are literally zero instructions executed on the CPU at runtime, whether it's doing training or inference. It will be blindingly fast for your users.

If only all code could be like that!

The reality is that programmers are still needed and that code still needs to run (sigh!). But to make it faster, there are lots of ways to have more computation done by the compiler, long before it ever goes near a user.

The C++ programming language has numerous features that help perform work at compile-time. These include ways to explicitly control what goes to the compiler, or to give more information to the compiler so that its optimizer can do good work on your behalf. Some of the various C++ language features to consider include:

  • Conditional compilation — #if/#ifdef statements
  • inline functions
  • Templates — these expand at compile-time
  • Symbolic constants — const or #define
  • Function-like macros — #define with parameters
  • Constant hints — constexpr, if constexpr, etc.
  • Global and static variable initializations
  • static data members — fixed data in C++ classes
  • Type traits — compile-time type testing
  • Restricted pointers — ignore aliasing risks

But when we're doing AI, there's another compile-time data structure to consider: the whole LLM model itself.

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++