Aussie AI

Assertions

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

Assertions

Of all the self-testing code techniques, my favorite one is definitely assertions. They're just so easy to add!

The use of assertions in AI programs is not much different to any other C++ coding task. Assertions can be a very valuable part of improving the quality of your work over the long term. I find them especially useful in getting rid of obvious glitches when I'm writing new code, but then I usually leave them in there.

The standard C++ library has had an “assert” macro since back when it was called C. The assert macro is one convenient method of performing simple tests, and larger code fragments can be used for more complicated tests. The basic usage is illustrated to check the inputs of a vector function:

    #include <assert.h>
    ...
    float vector_sum_assert_example1(float v[], int n)
    {
        assert(v != NULL);  // Easy!
        float sum = 0;
        for (int i = 0; i < n; i++) {
            sum += v[i];
        }
        return sum;
    }

 

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++