Loading...
Searching...
No Matches
inference_model.c
Go to the documentation of this file.
20 * @details Descriptors bind preprocessing to graph bytes; application adapters still qualify semantic outputs. */
46/** @brief Store an owned bounded name so parser-buffer destruction cannot invalidate tensor views. */
58/** @brief Accept concrete positive extents only; export-time dynamic axes need an explicit execution shape. */
75/** @brief Accept one shared channel value or exactly three, without silently broadcasting an incomplete pair. */
98/** @brief Parse one indexed tensor while keeping duplicate detection separate for each tensor and direction. */
99static bool tensor_field(struct model_parser *parser, const char *key, const char *value, bool input)
104 if (!separator || separator == number || (size_t)(separator - number) >= sizeof(index_text)) return false;
111 struct onnx_tensor *tensor = input ? &parser->model->inputs[slot] : &parser->model->outputs[slot];
128 static const enum onnx_element_type types[] = {ONNX_FLOAT32, ONNX_UINT8, ONNX_INT8, ONNX_INT32, ONNX_INT64, ONNX_FLOAT64};
135/** @brief Recognize descriptor policy, not arbitrary network-specific configuration or custom operators. */
142 static const char *const names[] = {"schema", "adapter", "model_sha256", "input_count", "output_count",
147 if (field == sizeof(names) / sizeof(names[0]) || (parser->seen & (UINT32_C(1) << field))) return false;
153 if (field == 15) return !strchr(value, ';') && copy_name(model->labels, sizeof(model->labels), value);
156 if (!ini_uint(value, &number) || !number || number > (field < 5 ? ONNX_TENSOR_LIMIT : 192000U)) return false;
174 } else if (field == 8 || field == 9) return parse_channels(field == 8 ? model->image.scale : model->image.mean, value);
194 /* Hash the original bytes before destructive parsing so evidence identifies the exact preprocessing contract. */
197 if (!ini_parse(contents, length, model_field, &parser) || (parser.seen & 31U) != 31U) goto invalid;
201 if (parser.inputs[slot] != (input ? 7 : 0) || parser.outputs[slot] != (output ? 7 : 0)) goto invalid;
210 if (!strcmp(tensor->name, side ? model->outputs[previous].name : model->inputs[previous].name)) goto invalid;
217 if ((parser.seen & required) != required || input->rank != 4 || input->dimensions[0] != 1) goto invalid;
221 if (input->dimensions[channel_axis] != model->image.channels || input->dimensions[width_axis] > 2048 ||
226 if (input->type != ONNX_FLOAT32 && input->type != ONNX_UINT8 && input->type != ONNX_INT8) goto invalid;
288 if (!inference_model_read(&context->model, settings->descriptor, settings->model, error, capacity)) return false;
290 snprintf(error, capacity, "Unsupported model adapter: %s (expected %s)", context->model.adapter, adapter);
294 /* Align every tensor independently; mixed numeric types may otherwise receive misaligned storage. */
311 /* POSIX-backed private mapping avoids anonymous-mapping extensions and gives tensor storage a clear lifetime. */
338 if (!onnx_session_open(&context->session, settings->model, context->model.inputs, context->model.input_count,
357 return onnx_session_run(&context->session, context->timeout_ms ? now + (uint64_t)context->timeout_ms * 1000 : 0,
365 if (!context || !context->storage || context->model.input_count != 1 || !context->model.image.channels) {
static bool ini_uint(const char *text, uint64_t *number)
Parse digits-only uint64; overflow and signs are rejected.
Definition bounded_ini.h:52
static bool ini_real(const char *text, double *number)
Parse finite decimal/exponent notation, rejecting range errors and suffixes.
Definition bounded_ini.h:67
static bool ini_read_file(const char *filename, char *buffer, size_t capacity, size_t *length)
Read a nonempty regular file smaller than capacity and append NUL.
Definition bounded_ini.h:98
static bool ini_parse(char *buffer, size_t length, ini_field_handler handler, void *context)
Destructively parse flat fields with blank lines and #/; comment lines.
Definition bounded_ini.h:132
bool inference_image_prepare(const struct inference_image *image, const struct inference_image_spec *spec, void *tensor, size_t capacity, struct inference_image_transform *transform, char *error, size_t error_capacity)
Convert with the declared nearest-neighbor policy, not a guessed training transform.
Definition inference_image.c:69
bool inference_context_run(struct inference_context *context, char *error, size_t capacity)
Run the currently prepared tensors using the context's timeout policy.
Definition inference_model.c:346
static bool copy_name(char *destination, size_t capacity, const char *value)
Store an owned bounded name so parser-buffer destruction cannot invalidate tensor views.
Definition inference_model.c:47
static bool parse_channels(float values[3], const char *text)
Accept one shared channel value or exactly three, without silently broadcasting an incomplete pair.
Definition inference_model.c:76
bool inference_context_open(struct inference_context *context, const struct inference_settings *settings, const char *adapter, char *error, size_t capacity)
Verify a bundle, map bounded tensors and bind one warm session.
Definition inference_model.c:277
bool inference_context_prepare_image(struct inference_context *context, const struct inference_image *image, char *error, size_t capacity)
Prepare a single image input without running the model, so camera leases can be released first.
Definition inference_model.c:361
bool inference_file_sha256(const char *filename, char digest[65])
Hash a stable nonempty regular file using bounded streaming scratch.
Definition inference_model.c:41
static bool model_field(void *context, const char *key, const char *value)
Recognize descriptor policy, not arbitrary network-specific configuration or custom operators.
Definition inference_model.c:136
bool inference_model_read(struct inference_model *model, const char *descriptor, const char *graph, char *error, size_t capacity)
Load a bounded descriptor and verify its exact graph digest; hashes are not authentication.
Definition inference_model.c:258
bool inference_model_label(const struct inference_model *model, unsigned category, char *label, size_t capacity)
Resolve one model-owned label without retaining a pointer into a mutable list.
Definition inference_model.c:237
void inference_context_close(struct inference_context *context)
Release the session before unmapping its borrowed tensor storage.
Definition inference_model.c:373
bool inference_model_parse(struct inference_model *model, char *contents, size_t length, char *error, size_t capacity)
Parse a descriptor, not graph bytes; graph integrity remains a separate read-time check.
Definition inference_model.c:189
static bool parse_dimensions(struct onnx_tensor *tensor, const char *text)
Accept concrete positive extents only; export-time dynamic axes need an explicit execution shape.
Definition inference_model.c:59
static bool tensor_field(struct model_parser *parser, const char *key, const char *value, bool input)
Parse one indexed tensor while keeping duplicate detection separate for each tensor and direction.
Definition inference_model.c:99
Integrity-checked model contracts and independently owned tensor workspaces.
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.
Definition onnx_session.c:47
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.
Definition onnx_session.c:251
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.
Definition onnx_session.c:187
uint64_t onnx_monotonic_us(void)
Read the clock used by inference deadlines.
Definition onnx_session.c:40
void onnx_session_close(struct onnx_session *session)
Cancel/wait for an active run and release resources; repeated closes are harmless.
Definition onnx_session.c:337
onnx_element_type
Supported numeric types; values match the ONNX Runtime C ABI.
Definition onnx_session.h:38
bool sha256_file(const char *filename, char output[SHA256_HEX_SIZE])
Hash an immutable nonempty regular file with bounded streaming scratch.
Definition sha256fst.c:286
void sha256_hex(const void *data, size_t length, char *output)
Hash one memory region to a lowercase hexadecimal string.
Definition sha256fst.c:272
Allocation-free, portable SHA-256 checksums for memory and streams.
Independent, noncopyable model owner; mapped tensor buffers and session are reused between jobs.
Definition inference_model.h:43
struct inference_image_transform transform
Definition inference_model.h:46
Borrowed immutable planes; lengths/strides prove all addressed rows are available.
Definition inference_image.h:37
Parsed descriptor with self-referencing tensor-name pointers; do not copy after parsing.
Definition inference_model.h:31
char input_names[ONNX_TENSOR_LIMIT][ONNX_NAME_CAPACITY]
Definition inference_model.h:34
char output_names[ONNX_TENSOR_LIMIT][ONNX_NAME_CAPACITY]
Definition inference_model.h:35
struct onnx_tensor outputs[ONNX_TENSOR_LIMIT]
Definition inference_model.h:36
Per-application configuration; all resolved paths belong to this caller-owned value.
Definition inference_settings.h:39
char descriptor[INFERENCE_PATH_CAPACITY]
Definition inference_settings.h:43
char library_directory[INFERENCE_PATH_CAPACITY]
Definition inference_settings.h:44
Independent duplicate masks for descriptor-wide and indexed tensor fields.
Definition inference_model.c:35
Explicit execution limits so each model cannot create an unbounded CPU pool.
Definition onnx_session.h:54
Named concrete tensor view; its aligned backing storage must outlive the session.
Definition onnx_session.h:44