Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
onnx_session.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/** @file onnx_session.c
19 * @brief Reusable numeric tensor sessions without image, acoustic or planning semantics.
20 * @details Cancellation is cooperative; outer worker supervision remains the recovery boundary. */
21#include "onnx_session.h"
22#include "onnx_runtime.h"
23
24#include <onnxruntime_c_api.h>
25#include <errno.h>
26#include <stdio.h>
27#include <string.h>
28#include <time.h>
29
30/** @brief Consume every ORT error object exactly once while copying its diagnostic before release. */
31static bool checked(OrtStatus *status, char *error, size_t capacity)
32{
33 if (!status) return true;
34 const OrtApi *api = onnx_runtime_api();
35 snprintf(error, capacity, "%s", api->GetErrorMessage(status));
36 api->ReleaseStatus(status);
37 return false;
38}
39
40uint64_t onnx_monotonic_us(void)
41{
42 struct timespec now;
43 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0 || now.tv_sec < 0) return 0;
44 return (uint64_t)now.tv_sec * UINT64_C(1000000) + (uint64_t)now.tv_nsec / 1000U;
45}
46
47bool onnx_tensor_bytes(enum onnx_element_type type, const int64_t *dimensions,
48 size_t rank, size_t *bytes)
49{
50 if (!bytes || rank > ONNX_RANK_LIMIT || (rank && !dimensions)) return false;
51 size_t size;
52 switch (type) {
53 case ONNX_FLOAT32: case ONNX_INT32: size = 4; break;
54 case ONNX_FLOAT64: case ONNX_INT64: size = 8; break;
55 case ONNX_UINT8: case ONNX_INT8: size = 1; break;
56 default: return false;
57 }
58 for (size_t axis = 0; axis < rank; ++axis) {
59 /* Divide before multiplying so malformed dimensions cannot wrap a valid-looking allocation size. */
60 if (dimensions[axis] <= 0 || (uint64_t)dimensions[axis] > ONNX_BUFFER_LIMIT / size) return false;
61 size *= (size_t)dimensions[axis];
62 }
63 *bytes = size;
64 return true;
65}
66
67/** @brief Compare concrete execution shapes, allowing dynamic graph axes only during initial binding. */
68static bool tensor_contract(const OrtTensorTypeAndShapeInfo *info,
69 const struct onnx_tensor *spec, bool dynamic,
70 char *error, size_t capacity)
71{
72 const OrtApi *api = onnx_runtime_api();
73 ONNXTensorElementDataType type;
74 size_t rank = 0;
75 int64_t dimensions[ONNX_RANK_LIMIT];
76 if (!checked(api->GetTensorElementType(info, &type), error, capacity) ||
77 !checked(api->GetDimensionsCount(info, &rank), error, capacity)) return false;
78 if ((int)type != (int)spec->type || rank != spec->rank || rank > ONNX_RANK_LIMIT) {
79 snprintf(error, capacity, "Unexpected model tensor type/rank: %s", spec->name);
80 return false;
81 }
82 if (!checked(api->GetDimensions(info, dimensions, rank), error, capacity)) return false;
83 for (size_t axis = 0; axis < rank; ++axis) {
84 if (dimensions[axis] != spec->dimensions[axis] && !(dynamic && dimensions[axis] == -1)) {
85 snprintf(error, capacity, "Unexpected model tensor dimension: %s", spec->name);
86 return false;
87 }
88 }
89 return true;
90}
91
92/** @brief Bind by graph name rather than declaration order, which may change during export optimization. */
93static bool bind_tensors(struct onnx_session *session, const struct onnx_tensor *specs,
94 size_t count, bool input, char *error, size_t capacity)
95{
96 const OrtApi *api = onnx_runtime_api();
97 OrtAllocator *allocator = NULL;
98 size_t actual_count = 0;
99 if (!checked(api->GetAllocatorWithDefaultOptions(&allocator), error, capacity) ||
100 !checked(input ? api->SessionGetInputCount(session->handle, &actual_count) :
101 api->SessionGetOutputCount(session->handle, &actual_count), error, capacity)) return false;
102 if (actual_count != count) {
103 snprintf(error, capacity, "Unexpected model input/output count");
104 return false;
105 }
106 bool seen[ONNX_TENSOR_LIMIT] = {false};
107 size_t total_bytes = 0;
108 for (size_t index = 0; index < count; ++index) {
109 char *name = NULL;
110 if (!checked(input ? api->SessionGetInputName(session->handle, index, allocator, &name) :
111 api->SessionGetOutputName(session->handle, index, allocator, &name), error, capacity)) return false;
112 size_t slot = 0;
113 while (slot < count && (!specs[slot].name || strcmp(name, specs[slot].name))) ++slot;
114 OrtStatus *released = api->AllocatorFree(allocator, name);
115 if (!checked(released, error, capacity)) return false;
116 if (slot == count || seen[slot]) {
117 snprintf(error, capacity, "Unexpected model %s name", input ? "input" : "output");
118 return false;
119 }
120 seen[slot] = true;
121 const struct onnx_tensor *spec = &specs[slot];
122 size_t bytes = 0;
123 if (!spec->name[0] || strlen(spec->name) >= ONNX_NAME_CAPACITY || !spec->data ||
124 !onnx_tensor_bytes(spec->type, spec->dimensions, spec->rank, &bytes) || spec->bytes != bytes ||
125 bytes > ONNX_BUFFER_LIMIT - total_bytes) {
126 snprintf(error, capacity, "Invalid tensor storage contract");
127 return false;
128 }
129 total_bytes += bytes;
130 OrtTypeInfo *type = NULL;
131 if (!checked(input ? api->SessionGetInputTypeInfo(session->handle, index, &type) :
132 api->SessionGetOutputTypeInfo(session->handle, index, &type), error, capacity)) return false;
133 const OrtTensorTypeAndShapeInfo *info = NULL;
134 const bool valid = checked(api->CastTypeInfoToTensorInfo(type, &info), error, capacity) && info &&
135 tensor_contract(info, spec, true, error, capacity);
136 api->ReleaseTypeInfo(type);
137 if (!valid) {
138 if (!error[0]) snprintf(error, capacity, "Expected model tensor: %s", spec->name);
139 return false;
140 }
141 OrtValue *value = NULL;
142 if (!checked(api->CreateTensorWithDataAsOrtValue(session->memory, spec->data, bytes,
143 spec->dimensions, spec->rank, (ONNXTensorElementDataType)spec->type, &value), error, capacity)) return false;
144 char *destination = input ? session->input_names[slot] : session->output_names[slot];
145 for (size_t character = 0; character <= strlen(spec->name); ++character) destination[character] = spec->name[character];
146 if (input) session->inputs[slot] = value;
147 else {
148 session->outputs[slot] = value;
149 struct onnx_tensor *output = &session->output_specs[slot];
150 output->name = destination;
151 output->type = spec->type;
152 output->rank = spec->rank;
153 output->data = spec->data;
154 output->bytes = spec->bytes;
155 for (size_t axis = 0; axis < spec->rank; ++axis) output->dimensions[axis] = spec->dimensions[axis];
156 }
157 }
158 return true;
159}
160
161/** @brief Request termination without releasing buffers still borrowed by a running graph. */
162static void *watch_deadline(void *context)
163{
164 struct onnx_session *session = context;
165 pthread_mutex_lock(&session->mutex);
166 while (!session->stopping) {
167 if (!session->running || !session->deadline_us || session->timed_out) {
168 pthread_cond_wait(&session->condition, &session->mutex);
169 continue;
170 }
171 const struct timespec deadline = {
172 .tv_sec = (time_t)(session->deadline_us / UINT64_C(1000000)),
173 .tv_nsec = (long)(session->deadline_us % UINT64_C(1000000)) * 1000L
174 };
175 const int result = pthread_cond_timedwait(&session->condition, &session->mutex, &deadline);
176 if (result == ETIMEDOUT && session->running && session->deadline_us &&
177 onnx_monotonic_us() >= session->deadline_us) {
178 session->timed_out = true;
179 OrtStatus *status = onnx_runtime_api()->RunOptionsSetTerminate(session->run_options);
180 if (status) onnx_runtime_api()->ReleaseStatus(status);
181 }
182 }
183 pthread_mutex_unlock(&session->mutex);
184 return NULL;
185}
186
187bool onnx_session_open(struct onnx_session *session, const char *model,
188 const struct onnx_tensor *inputs, size_t input_count,
189 const struct onnx_tensor *outputs, size_t output_count,
190 const struct onnx_session_options *options,
191 char *error, size_t capacity)
192{
193 if (!session || !error || !capacity) return false;
194 error[0] = '\0';
195 if (session->handle || session->synchronized || !model || !model[0] || !inputs || !outputs ||
196 !input_count || input_count > ONNX_TENSOR_LIMIT || !output_count || output_count > ONNX_TENSOR_LIMIT ||
197 (options && (!options->threads || options->threads > 32))) {
198 snprintf(error, capacity, "Invalid ONNX session configuration");
199 return false;
200 }
201 if (!onnx_runtime_open(options ? options->library_directory : NULL, error, capacity)) return false;
202 const OrtApi *api = onnx_runtime_api();
203 OrtSessionOptions *configuration = NULL;
204 OrtSession *handle = NULL;
205 OrtMemoryInfo *memory = NULL;
206 OrtRunOptions *run_options = NULL;
207 if (!checked(api->CreateSessionOptions(&configuration), error, capacity)) return false;
208 const unsigned threads = options ? options->threads : 1;
209 /* Per-model default pools otherwise compete for every core and keep the small board spinning. */
210 bool valid = checked(api->SetIntraOpNumThreads(configuration, (int)threads), error, capacity) &&
211 checked(api->SetInterOpNumThreads(configuration, 1), error, capacity) &&
212 checked(api->SetSessionExecutionMode(configuration, ORT_SEQUENTIAL), error, capacity) &&
213 checked(api->SetSessionGraphOptimizationLevel(configuration, ORT_ENABLE_ALL), error, capacity) &&
214 checked(api->AddSessionConfigEntry(configuration, "session.intra_op.allow_spinning", "0"), error, capacity) &&
215 checked(api->AddSessionConfigEntry(configuration, "session.inter_op.allow_spinning", "0"), error, capacity) &&
216 checked(api->CreateSession(onnx_runtime_environment(), model, configuration, &handle), error, capacity);
217 api->ReleaseSessionOptions(configuration);
218 if (!valid) return false;
219 session->handle = handle;
220 session->input_count = input_count;
221 session->output_count = output_count;
222 valid = checked(api->CreateCpuMemoryInfo(OrtArenaAllocator, OrtMemTypeDefault, &memory), error, capacity) &&
223 checked(api->CreateRunOptions(&run_options), error, capacity);
224 session->memory = memory;
225 session->run_options = run_options;
226 if (!valid || !bind_tensors(session, inputs, input_count, true, error, capacity) ||
227 !bind_tensors(session, outputs, output_count, false, error, capacity)) goto failed;
228 if (pthread_mutex_init(&session->mutex, NULL) != 0) goto synchronization_failed;
229 pthread_condattr_t attributes;
230 if (pthread_condattr_init(&attributes) != 0) {
231 pthread_mutex_destroy(&session->mutex);
232 goto synchronization_failed;
233 }
234 /* The waiter and public deadlines must share a clock unaffected by wall-clock corrections. */
235 const int configured = pthread_condattr_setclock(&attributes, CLOCK_MONOTONIC);
236 const int initialized = configured ? configured : pthread_cond_init(&session->condition, &attributes);
237 pthread_condattr_destroy(&attributes);
238 if (initialized) {
239 pthread_mutex_destroy(&session->mutex);
240 goto synchronization_failed;
241 }
242 session->synchronized = true;
243 return true;
244synchronization_failed:
245 snprintf(error, capacity, "Cannot initialize ONNX session synchronization");
246failed:
248 return false;
249}
250
251bool onnx_session_run(struct onnx_session *session, uint64_t deadline_us,
252 char *error, size_t capacity)
253{
254 if (!session || !error || !capacity) return false;
255 error[0] = '\0';
256 if (!session->handle || !session->synchronized) {
257 snprintf(error, capacity, "ONNX session is not open");
258 return false;
259 }
260 const OrtApi *api = onnx_runtime_api();
261 pthread_mutex_lock(&session->mutex);
262 if (session->running || session->stopping || (deadline_us && onnx_monotonic_us() >= deadline_us)) {
263 pthread_mutex_unlock(&session->mutex);
264 snprintf(error, capacity, "ONNX session busy, stopped or deadline expired");
265 return false;
266 }
267 if (deadline_us && !session->watched) {
268 /* Lazily create one monitor only when this session first needs a deadline. */
269 const int started = pthread_create(&session->watchdog, NULL, watch_deadline, session);
270 if (started) {
271 pthread_mutex_unlock(&session->mutex);
272 snprintf(error, capacity, "Cannot start ONNX deadline monitor: %s", strerror(started));
273 return false;
274 }
275 session->watched = true;
276 }
277 if (!checked(api->RunOptionsUnsetTerminate(session->run_options), error, capacity)) {
278 pthread_mutex_unlock(&session->mutex);
279 return false;
280 }
281 session->timed_out = false;
282 session->cancelled = false;
283 session->deadline_us = deadline_us;
284 session->running = true;
285 pthread_cond_signal(&session->condition);
286 pthread_mutex_unlock(&session->mutex);
287 const char *input_names[ONNX_TENSOR_LIMIT], *output_names[ONNX_TENSOR_LIMIT];
288 const OrtValue *inputs[ONNX_TENSOR_LIMIT];
289 OrtValue *outputs[ONNX_TENSOR_LIMIT];
290 for (size_t index = 0; index < session->input_count; ++index) {
291 input_names[index] = session->input_names[index];
292 inputs[index] = session->inputs[index];
293 }
294 for (size_t index = 0; index < session->output_count; ++index) {
295 output_names[index] = session->output_names[index];
296 outputs[index] = session->outputs[index];
297 }
298 bool valid = checked(api->Run(session->handle, session->run_options, input_names, inputs,
299 session->input_count, output_names, session->output_count, outputs), error, capacity);
300 /* Initial graph metadata alone cannot qualify outputs whose graph axes are dynamic. */
301 for (size_t index = 0; valid && index < session->output_count; ++index) {
302 OrtTensorTypeAndShapeInfo *info = NULL;
303 if (!checked(api->GetTensorTypeAndShape(outputs[index], &info), error, capacity)) valid = false;
304 else {
305 valid = tensor_contract(info, &session->output_specs[index], false, error, capacity);
306 api->ReleaseTensorTypeAndShapeInfo(info);
307 }
308 }
309 pthread_mutex_lock(&session->mutex);
310 if (session->timed_out || (deadline_us && onnx_monotonic_us() >= deadline_us)) {
311 snprintf(error, capacity, "ONNX inference deadline expired");
312 valid = false;
313 }
314 if (session->cancelled || session->stopping) {
315 snprintf(error, capacity, "ONNX inference cancelled");
316 valid = false;
317 }
318 session->running = false;
319 session->deadline_us = 0;
320 pthread_cond_broadcast(&session->condition);
321 pthread_mutex_unlock(&session->mutex);
322 return valid;
323}
324
326{
327 if (!session || !session->synchronized) return;
328 pthread_mutex_lock(&session->mutex);
329 if (session->running) {
330 session->cancelled = true;
331 OrtStatus *status = onnx_runtime_api()->RunOptionsSetTerminate(session->run_options);
332 if (status) onnx_runtime_api()->ReleaseStatus(status);
333 }
334 pthread_mutex_unlock(&session->mutex);
335}
336
338{
339 if (!session) return;
340 if (session->synchronized) {
341 pthread_mutex_lock(&session->mutex);
342 session->stopping = true;
343 if (session->running) {
344 session->cancelled = true;
345 OrtStatus *status = onnx_runtime_api()->RunOptionsSetTerminate(session->run_options);
346 if (status) onnx_runtime_api()->ReleaseStatus(status);
347 /* Termination is not completion; unmapping caller buffers before Run returns would race ORT. */
348 while (session->running) pthread_cond_wait(&session->condition, &session->mutex);
349 }
350 pthread_cond_broadcast(&session->condition);
351 pthread_mutex_unlock(&session->mutex);
352 if (session->watched) pthread_join(session->watchdog, NULL);
353 pthread_cond_destroy(&session->condition);
354 pthread_mutex_destroy(&session->mutex);
355 }
356 const OrtApi *api = onnx_runtime_api();
357 if (api) {
358 for (size_t index = 0; index < ONNX_TENSOR_LIMIT; ++index) {
359 if (session->inputs[index]) api->ReleaseValue(session->inputs[index]);
360 if (session->outputs[index]) api->ReleaseValue(session->outputs[index]);
361 }
362 if (session->run_options) api->ReleaseRunOptions(session->run_options);
363 if (session->memory) api->ReleaseMemoryInfo(session->memory);
364 if (session->handle) api->ReleaseSession(session->handle);
365 }
366 *session = (struct onnx_session){0};
367}
static unsigned capacity[COVERAGE_CAMERAS]
static uint32_t session
size_t size
Definition chdk_pipe.c:143
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
static bool started
struct jpeg_error_mgr error
Definition lwir_image.c:60
bool onnx_runtime_open(const char *library_directory, char *error, size_t capacity)
Initialize one process-lifetime runtime/environment using explicit local search roots.
OrtEnv * onnx_runtime_environment(void)
Borrow the environment retained until process exit.
const OrtApi * onnx_runtime_api(void)
Borrow the initialized API, without transferring release ownership.
Process-wide runtime ownership shared by independent model sessions.
bool onnx_tensor_bytes(enum onnx_element_type type, const int64_t *dimensions, size_t rank, size_t *bytes)
Compute bounded concrete storage, rejecting unsupported types, dynamic extents and overflow.
static bool checked(OrtStatus *status, char *error, size_t capacity)
Consume every ORT error object exactly once while copying its diagnostic before release.
bool onnx_session_run(struct onnx_session *session, uint64_t deadline_us, char *error, size_t capacity)
Run once; deadline is absolute CLOCK_MONOTONIC microseconds, or zero for none.
bool onnx_session_open(struct onnx_session *session, const char *model, const struct onnx_tensor *inputs, size_t input_count, const struct onnx_tensor *outputs, size_t output_count, const struct onnx_session_options *options, char *error, size_t capacity)
Validate graph contracts and bind reusable caller buffers; failure releases acquired resources.
static void * watch_deadline(void *context)
Request termination without releasing buffers still borrowed by a running graph.
static bool tensor_contract(const OrtTensorTypeAndShapeInfo *info, const struct onnx_tensor *spec, bool dynamic, char *error, size_t capacity)
Compare concrete execution shapes, allowing dynamic graph axes only during initial binding.
uint64_t onnx_monotonic_us(void)
Read the clock used by inference deadlines.
void onnx_session_cancel(struct onnx_session *session)
Request cooperative cancellation from another thread; join the owner before destroying storage.
static bool bind_tensors(struct onnx_session *session, const struct onnx_tensor *specs, size_t count, bool input, char *error, size_t capacity)
Bind by graph name rather than declaration order, which may change during export optimization.
void onnx_session_close(struct onnx_session *session)
Cancel/wait for an active run and release resources; repeated closes are harmless.
Bounded tensor bindings and cooperative execution policy, independent of CAM semantics.
#define ONNX_NAME_CAPACITY
#define ONNX_TENSOR_LIMIT
onnx_element_type
Supported numeric types; values match the ONNX Runtime C ABI.
@ ONNX_INT32
@ ONNX_FLOAT32
@ ONNX_FLOAT64
@ ONNX_INT64
@ ONNX_UINT8
@ ONNX_INT8
#define ONNX_BUFFER_LIMIT
#define ONNX_RANK_LIMIT
static size_t count
Definition pose_log.c:79
static FILE * output
Definition pose_log.c:77
Explicit execution limits so each model cannot create an unbounded CPU pool.
Noncopyable caller-owned session; zero-initialize and serialize open/run/close ownership.
Named concrete tensor view; its aligned backing storage must outlive the session.
enum onnx_element_type type
int64_t dimensions[ONNX_RANK_LIMIT]
const char * name
Validated capture and reporting policy, fixed before the sample loop.
Definition earcam.c:137
bool valid
Definition usher.c:537
struct usher_control_result result
Definition usher.c:377