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
loom_onnx_features.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 loom_onnx_features.c
19 * @brief Model-identified feature transport independent of IMX500 tensor metadata.
20 * @details Fixed little-endian fields preserve float bits and feature-space identity across PC/ARM64. */
21#include "loom_features.h"
23
24#include <math.h>
25#include <string.h>
26
27/** @brief Encode an already-bounds-checked field without unaligned integer stores. */
28static void write_u32(unsigned char *destination, uint32_t value)
29{
30 for (unsigned index = 0; index < 4; ++index) destination[index] = (unsigned char)(value >> (8 * index));
31}
32
33/** @brief Decode an already-bounds-checked field without host-endian or alignment assumptions. */
34static uint32_t read_u32(const unsigned char *source)
35{
36 return (uint32_t)source[0] | (uint32_t)source[1] << 8 | (uint32_t)source[2] << 16 | (uint32_t)source[3] << 24;
37}
38
39size_t loom_onnx_features_pack(unsigned char *record, size_t capacity, const struct inference_model *model,
40 uint64_t sequence, uint64_t sensor_timestamp_ns)
41{
42 if (!record || !model || !model->output_count || model->output_count > ONNX_TENSOR_LIMIT ||
43 strnlen(model->graph_sha256, sizeof(model->graph_sha256)) != 64 ||
44 strnlen(model->descriptor_sha256, sizeof(model->descriptor_sha256)) != 64) return 0;
45 for (size_t index = 0; index < 64; ++index) {
46 const unsigned char graph = (unsigned char)model->graph_sha256[index];
47 const unsigned char descriptor = (unsigned char)model->descriptor_sha256[index];
48 if (!((graph >= '0' && graph <= '9') || (graph >= 'a' && graph <= 'f')) ||
49 !((descriptor >= '0' && descriptor <= '9') || (descriptor >= 'a' && descriptor <= 'f'))) return 0;
50 }
51 size_t size = 156, total = 0;
52 /* Validate every output before touching the destination so a rejected feature set leaves no partial record. */
53 for (size_t index = 0; index < model->output_count; ++index) {
54 const struct onnx_tensor *tensor = &model->outputs[index];
55 size_t bytes = 0;
56 if (!tensor->name || !tensor->data || tensor->type != ONNX_FLOAT32 || !tensor->rank ||
57 !onnx_tensor_bytes(tensor->type, tensor->dimensions, tensor->rank, &bytes) || bytes != tensor->bytes ||
58 bytes / sizeof(float) > 4096 - total) return 0;
59 const size_t length = strnlen(tensor->name, ONNX_NAME_CAPACITY);
60 if (!length || length == ONNX_NAME_CAPACITY) return 0;
61 for (size_t character = 0; character < length; ++character)
62 if ((unsigned char)tensor->name[character] < 32 || (unsigned char)tensor->name[character] == 127) return 0;
63 for (size_t axis = 0; axis < tensor->rank; ++axis) if (tensor->dimensions[axis] > UINT32_MAX) return 0;
64 const float *values = tensor->data;
65 for (size_t value = 0; value < bytes / sizeof(float); ++value) if (!isfinite(values[value])) return 0;
66 total += bytes / sizeof(float);
67 size += 12 + length + tensor->rank * 4 + bytes;
68 }
69 if (size > capacity || size > LOOM_ONNX_FEATURE_CAPACITY) return 0;
70 static const unsigned char signature[] = "LOOMOX01";
71 for (size_t index = 0; index < 8; ++index) record[index] = signature[index];
72 for (size_t index = 0; index < 64; ++index) {
73 record[8 + index] = (unsigned char)model->graph_sha256[index];
74 record[72 + index] = (unsigned char)model->descriptor_sha256[index];
75 }
76 for (unsigned index = 0; index < 8; ++index) {
77 record[136 + index] = (unsigned char)(sequence >> (8 * index));
78 record[144 + index] = (unsigned char)(sensor_timestamp_ns >> (8 * index));
79 }
80 write_u32(record + 152, (uint32_t)model->output_count);
81 size_t offset = 156;
82 for (size_t index = 0; index < model->output_count; ++index) {
83 const struct onnx_tensor *tensor = &model->outputs[index];
84 const size_t length = strlen(tensor->name);
85 write_u32(record + offset, (uint32_t)length); offset += 4;
86 for (size_t character = 0; character < length; ++character) record[offset++] = (unsigned char)tensor->name[character];
87 write_u32(record + offset, (uint32_t)tensor->rank); offset += 4;
88 for (size_t axis = 0; axis < tensor->rank; ++axis) {
89 write_u32(record + offset, (uint32_t)tensor->dimensions[axis]); offset += 4;
90 }
91 const size_t count = tensor->bytes / sizeof(float);
92 write_u32(record + offset, (uint32_t)count); offset += 4;
93 const float *values = tensor->data;
94 for (size_t value = 0; value < count; ++value) {
95 union { float value; uint32_t bits; } encoded = {.value = values[value]};
96 write_u32(record + offset, encoded.bits); offset += 4;
97 }
98 }
99 return offset;
100}
101
102bool loom_onnx_features_valid(const unsigned char *record, size_t size,
103 const char *model_sha256, const char *descriptor_sha256)
104{
105 if (!record || size < 156 || size > LOOM_ONNX_FEATURE_CAPACITY || memcmp(record, "LOOMOX01", 8)) return false;
106 for (size_t index = 8; index < 136; ++index)
107 if (!((record[index] >= '0' && record[index] <= '9') || (record[index] >= 'a' && record[index] <= 'f'))) return false;
108 /* Matching shapes alone cannot make descriptors from different trained feature spaces comparable. */
109 if (model_sha256 && (strnlen(model_sha256, 65) != 64 || memcmp(record + 8, model_sha256, 64))) return false;
110 if (descriptor_sha256 && (strnlen(descriptor_sha256, 65) != 64 || memcmp(record + 72, descriptor_sha256, 64))) return false;
111 const uint32_t tensors = read_u32(record + 152);
112 if (!tensors || tensors > ONNX_TENSOR_LIMIT) return false;
113 size_t offset = 156, total = 0;
114 for (uint32_t tensor = 0; tensor < tensors; ++tensor) {
115 if (size - offset < 4) return false;
116 const uint32_t name = read_u32(record + offset); offset += 4;
117 if (!name || name >= ONNX_NAME_CAPACITY || name > size - offset) return false;
118 for (uint32_t index = 0; index < name; ++index) if (record[offset + index] < 32 || record[offset + index] == 127) return false;
119 offset += name;
120 if (size - offset < 4) return false;
121 const uint32_t rank = read_u32(record + offset); offset += 4;
122 if (!rank || rank > ONNX_RANK_LIMIT || (size_t)rank * 4 > size - offset) return false;
123 uint32_t product = 1;
124 for (uint32_t axis = 0; axis < rank; ++axis) {
125 const uint32_t dimension = read_u32(record + offset); offset += 4;
126 if (!dimension || dimension > 4096 / product) return false;
127 product *= dimension;
128 }
129 if (size - offset < 4) return false;
130 const uint32_t count = read_u32(record + offset); offset += 4;
131 if (count != product || count > 4096 - total || (size_t)count * 4 > size - offset) return false;
132 total += count;
133 for (uint32_t value = 0; value < count; ++value) {
134 union { float value; uint32_t bits; } decoded = {.bits = read_u32(record + offset)};
135 if (!isfinite(decoded.value)) return false;
136 offset += 4;
137 }
138 }
139 return offset == size;
140}
141
142bool loom_onnx_return_vector(const unsigned char *record, size_t size,
143 const char *model_sha256, const char *descriptor_sha256, float values[3])
144{
145 if (!values || !loom_onnx_features_valid(record, size, model_sha256, descriptor_sha256) ||
146 read_u32(record + 152) != 1) return false;
147 size_t offset = 156;
148 const uint32_t name = read_u32(record + offset);
149 offset += 4 + name;
150 const uint32_t rank = read_u32(record + offset);
151 offset += 4 + rank * 4;
152 const uint32_t count = read_u32(record + offset);
153 offset += 4;
154 if (count != 2 && count != 3) return false;
155 float decoded[3] = {0, 0, 1};
156 for (uint32_t index = 0; index < count; ++index) {
157 union { uint32_t bits; float value; } number = {.bits = read_u32(record + offset + index * 4)};
158 decoded[index] = number.value;
159 }
160 for (unsigned index = 0; index < 3; ++index) values[index] = decoded[index];
161 return true;
162}
static unsigned capacity[COVERAGE_CAMERAS]
size_t size
Definition chdk_pipe.c:143
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
int descriptor
Definition chdk_pipe.c:142
static int record(const union dc_shot_union *shot)
Retrieve a point and admit a new observation only when its credit is free.
Integrity-checked model contracts and independently owned tensor workspaces.
LOOM-owned bounded feature validation and evidence serialization.
#define LOOM_ONNX_FEATURE_CAPACITY
static void write_u32(unsigned char *destination, uint32_t value)
Encode an already-bounds-checked field without unaligned integer stores.
bool loom_onnx_features_valid(const unsigned char *record, size_t size, const char *model_sha256, const char *descriptor_sha256)
Validate the exact binary layout and optionally require the same trained feature space.
size_t loom_onnx_features_pack(unsigned char *record, size_t capacity, const struct inference_model *model, uint64_t sequence, uint64_t sensor_timestamp_ns)
Encode a complete LOOMOX01 record without fabricating an IMX500 metadata header.
static uint32_t read_u32(const unsigned char *source)
Decode an already-bounds-checked field without host-endian or alignment assumptions.
bool loom_onnx_return_vector(const unsigned char *record, size_t size, const char *model_sha256, const char *descriptor_sha256, float values[3])
Decode an explicitly declared two/three-value return-vector record.
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.
#define ONNX_NAME_CAPACITY
#define ONNX_TENSOR_LIMIT
@ ONNX_FLOAT32
#define ONNX_RANK_LIMIT
static size_t count
Definition pose_log.c:79
Parsed descriptor with self-referencing tensor-name pointers; do not copy after parsing.
char descriptor_sha256[65]
struct onnx_tensor outputs[ONNX_TENSOR_LIMIT]
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