Struct anira::ContextConfig#
-
struct ContextConfig#
Configuration structure for the inference context and threading behavior.
The ContextConfig struct controls global settings for the anira inference system, including thread pool management and available inference backends. This configuration is shared across all inference sessions within a single context instance.
See also
- Usage Examples:
// Use default configuration (half of available CPU cores) anira::ContextConfig default_config; // Specify custom thread count anira::ContextConfig custom_config(4); // Use with InferenceHandler anira::InferenceHandler handler(pp_processor, inference_config, custom_config);
Note
This configuration affects global behavior and should be set once during application initialization. Changing context configuration during runtime requires recreating the context and all associated sessions.
Public Functions
-
inline ContextConfig(unsigned int num_threads = default_num_threads(), WaitStrategy wait_strategy = WaitStrategy::SpinBackoff, LogLevel log_level = default_log_level())#
Constructs a ContextConfig with specified thread count.
Initializes the context configuration with the given number of inference threads and automatically populates the list of available backends based on compile-time feature flags.
Note
The constructor automatically detects and registers available inference backends based on compile-time definitions (USE_LIBTORCH, USE_ONNXRUNTIME, USE_TFLITE)
- Parameters:
num_threads – Number of background inference threads to create. Default: half of available CPU cores (minimum 1) on native builds, 0 on WebAssembly (see default_num_threads()). Pass 0 to opt out of the auto-managed pool and supply your own threads via Context::make_inference_thread() (required on WebAssembly, optional on native). On WebAssembly a nonzero value is coerced to 0 with a warning by Context::get_instance and JsonConfigLoader — the context cannot run threads there; they are always supplied externally (e.g. AniraWeb.spinUpInferenceWorker()). When the Context singleton already exists, num_threads == 0 leaves any existing pool untouched — it signals “no preference,” not “shrink to zero.”
wait_strategy – How idle inference threads wait for new work. Default: WaitStrategy::SpinBackoff (see WaitStrategy for the trade-offs). Must be identical across all ContextConfigs in a process, since all sessions share one thread pool.
log_level – Minimum severity of log messages emitted by anira and its backends (see LogLevel). Default: LogLevel::Info in debug builds, LogLevel::Error in release builds.
Public Members
-
unsigned int m_num_threads#
Number of background inference threads.
Controls the size of the thread pool used for neural network inference. These threads run at high priority to minimize inference latency and are shared across all inference sessions within the context.
Note
This value is set during construction and cannot be changed without recreating the context. All inference sessions using this context will share the same thread pool.
-
WaitStrategy m_wait_strategy = WaitStrategy::SpinBackoff#
Idle-wait strategy of the inference threads.
Determines whether idle inference threads poll the inference queue with an exponential-backoff spin loop or block on the queue’s semaphore until work is enqueued. See WaitStrategy for the trade-offs.
Note
All sessions in a process share one inference thread pool, so only one strategy can be in effect per process: the one of the first-created context. A later ContextConfig requesting a different strategy is ignored and reported with a warning. On WebAssembly builds, Blocking is coerced to SpinBackoff with a warning (see WaitStrategy).
-
LogLevel m_log_level = default_log_level()#
Minimum severity of log messages emitted by anira and its backends.
Applied process-globally when the context is created and forwarded to the logging facilities of the backend runtimes (see LogLevel for details and the TFLite exemption). When ContextConfigs disagree, the lowest (most verbose) requested level wins and a warning is logged. Defaults to LogLevel::Info in debug builds and LogLevel::Error in release builds.
-
std::string m_anira_version = ANIRA_VERSION#
Version string of the anira library.
Contains the version of the anira library that was used to create this configuration. This is useful for debugging, logging, and ensuring compatibility when serializing/deserializing configurations.
Note
This field is automatically populated with the ANIRA_VERSION macro during construction and should not be modified manually.
-
std::vector<InferenceBackend> m_enabled_backends#
List of available inference backends.
Contains all inference backends that were detected as available during compilation. This list is automatically populated in the constructor based on compile-time feature flags:
InferenceBackend::LIBTORCH (if USE_LIBTORCH is defined)
InferenceBackend::ONNX (if USE_ONNXRUNTIME is defined)
InferenceBackend::TFLITE (if USE_TFLITE is defined)
InferenceBackend::CUSTOM (always available)
Note
The CUSTOM backend is not automatically added to this list but is always available for use with custom backend implementations.