Configuration and JSON files#
Configuration in code#
One 2.x anira::InferenceConfig becomes one anira::ModelConfig plus one Hard
anira::ContractHandle; one 2.x ContextConfig becomes one anira::ContextConfig, the same
role, the request the shared core reconciles (the 2.x Context’s statics are the process-wide
functions, The context); the anira::HostConfig handed to prepare becomes the host
blocks of the Hard contract and the anchor of the model config. The 3.x column gives the C++ builder
of <anira/anira.hpp> (cfg, spec, contract and context are the handles) with the
C entry beside it (Models and tensors, Contracts).
anira 2.x |
anira 3.x |
|---|---|
|
|
|
|
|
The |
|
One |
|
The extent of the tensor’s |
|
|
|
|
|
|
|
An explicit budget: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSON files#
The 2.x configuration file has two roots, context_config and inference_config, and mirrors
the 2.x structs:
{
"context_config": {
"num_threads": 1,
"wait_strategy": "spin_backoff",
"log": { "level": "warning", "drain": "thread", "queue_capacity": 512, "drain_interval_ms": 10 }
},
"inference_config": {
"model_data": [
{ "model_path": ".../simple_gain_network_mono.pt", "inference_backend": "LIBTORCH" },
{ "model_path": ".../simple_gain_network_mono.onnx", "inference_backend": "ONNX" },
{ "model_path": ".../simple_gain_network_mono.tflite", "inference_backend": "TFLITE" }
],
"tensor_shape": [
{
"input_shape": [[1, 1, 512], [1]],
"output_shape": [[1, 1, 512], [1]]
}
],
"processing_spec": {
"preprocess_input_channels": [1, 1],
"postprocess_output_channels": [1, 1],
"preprocess_input_size": [512, 0],
"postprocess_output_size": [512, 0]
},
"max_inference_time": 5.0,
"warm_up": 1
}
}
The 3.x loaders (anira_model_config_from_json / _from_json_file,
anira_context_config_from_json, anira_contract_from_json) recognise such a document by its
roots and upgrade it in memory, returning ANIRA_SUCCESS_UPGRADED: a success, so test with
ANIRA_FAILED(status), never with status != ANIRA_OK. One warning is logged per process.
Unlike the 2.x loader nothing is silently dropped: a malformed entry is ANIRA_ERROR_JSON with the
key path in anira_error::message.
2.x key |
3.x |
|---|---|
|
The context file’s |
|
The context file’s |
|
|
|
|
|
|
|
The extent of the |
|
|
|
|
|
|
|
|
|
Held back as a Hard contract ( |
any other key |
Stored as an extension of its host and refused by name at prepare. |
Tensors are named input_<i> / output_<i> and typed float32; anchor is left at its
default (the first streamed tensor), what the 2.x anira::HostConfig default did.
anira_error err = ANIRA_ERROR_INIT;
anira_model_config* cfg = NULL;
anira_status st = anira_model_config_from_json_file("Config.json", &cfg, &err);
if (ANIRA_FAILED(st)) { fprintf(stderr, "%s\n", err.message); return 1; }
anira_contract* legacy = NULL;
if (st == ANIRA_SUCCESS_UPGRADED) {
anira_model_config_take_legacy_contract(cfg, &legacy); /* max_inference_time, warm_up */
}
In C++ the same document gives the model config from its inference_config block, the Hard
contract that block held back, and the context config from its context_config block, the three
objects a handler runs the file with:
anira::ModelConfig model_config = anira::ModelConfig::from_file("Config.json");
anira::ContractHandle contract = model_config.take_legacy_contract().value(); // upgraded()
anira::ContextConfig context_config = anira::ContextConfig::from_file("Config.json");
anira::Context context(context_config);
anira::Pipeline pipeline;
pipeline.add_inference(model_config);
anira::InferenceHandler handler(context, pipeline);
Converting a file. Reading a 2.x file and writing the handle back is the migration tool:
anira_model_config_to_json and anira_context_config_to_json write the 3.x spelling with a
fixed key order. Both take (buf, cap, out_len) and return ANIRA_ERROR_BUFFER_TOO_SMALL with
the required length in out_len, so call once with a NULL buffer to size it. The contract has no
writer: write the {"hard": ...} file by hand from the values the legacy contract carries, which
ContractHandle::hard() reads back into an anira::Hard (the anira_contract_hard_* getters
in C; The three files shows the format).
size_t len = 0;
anira_model_config_to_json(cfg, NULL, 0, &len);
char* text = malloc(len + 1);
anira_model_config_to_json(cfg, text, len + 1, &len);
/* write text to model.json */
The 2.x C++ loader is removed with <anira/utils/JsonConfigLoader.h>. The 2.x document loads
through ModelConfig::from_file (with take_legacy_contract()) and ContextConfig::from_file,
or, in the 2.x spelling, through anira::v2::JsonConfigLoader of <anira/compat/v2.hpp>
(The classes and what differs from 2.x):
#include <anira/compat/v2.hpp>
anira::v2::JsonConfigLoader json_config_loader("path/to/Config.json");
anira::v2::ContextConfig context_config = std::move(*json_config_loader.get_context_config());
anira::v2::InferenceConfig inference_config =
std::move(*json_config_loader.get_inference_config());
The 2.x loader was lenient where the 3.x loaders are strict: it logged a malformed value and skipped
it, dropped an unparseable model_data or tensor_shape entry, and answered nullptr for a
configuration without model data, a tensor shape or max_inference_time. Each of these is an
anira::Error from the constructor now.