Aussie AI
Self-Test Block Macro with Debug Flags
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Self-Test Block Macro with Debug Flags
The compile-time on/off decision about self-testing code is not the most flexible method.
The block version of SELFTEST can also have levels or debug flag areas.
One natural extension is to implement a “flags” idiom for areas,
to allow configuration of what areas of self-testing code are executed for a particular run
(e.g. a decoding algorithm flag, a normalization flag, a MatMul flag, etc.).
One Boolean flag is set for each debugging area, which controls whether or not
the self-testing code in that module is enabled or not.
A macro definition of SELFTEST(flagarea) can be hooked into the run-time
configuration library for debugging output.
In this way, it has both a compile-out setting (YDEBUG==0)
and dynamic runtime “areas” for self-testing code.
Here's the definition of the self-testing code areas:
enum selftest_areas {
SELFTEST_NORMALIZATION,
SELFTEST_MATMUL,
SELFTEST_SOFTMAX,
// ... more
};
A use of the SELFTEST method with areas looks like:
YSELFTEST(SELFTEST_NORMALIZATION) {
// ... self-test code
}
The SELFTEST macro definition with area flags looks like:
extern bool g_aussie_debug_enabled; // Global override
extern bool YDEBUG_FLAGS[100]; // Area flags
#if YDEBUG
#define SELFTEST(flagarea) \
if(g_aussie_debug_enabled == 0 || YDEBUG_FLAGS[flagarea] == 0) \
{ /* do nothing */ } else
#else
#define SELFTEST if(1) {} else // disabled completely
#endif
This uses a “debug flags” array idea as for the debugging output commands, rather than a single “level” of debugging. Naturally, a better implementation would allow separation of the areas for debug trace output and self-testing code, with two different sets of levels/flags, but this is left as an extension for the reader.
|
• Next: • Up: Table of Contents |
|
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |