Line data Source code
1 : /**
2 : * @file cfdnn.c
3 : * @brief Technology-agnostic dispatcher for `.cfdnn` inference.
4 : *
5 : * Holds the model and context lifecycles, selects a backend implementation
6 : * table, and runs the layer loop. There are no architecture #ifdefs here: per
7 : * the project's backend-abstraction rule those belong in the backend files
8 : * only, and this file must read the same on every platform.
9 : */
10 :
11 : #include "cfdnn_internal.h"
12 :
13 : #include "cfd/core/cfd_status.h"
14 : #include "cfd/core/memory.h"
15 :
16 : #include <math.h>
17 : #include <string.h>
18 :
19 : /* ==========================================================================
20 : * Model
21 : * ========================================================================== */
22 :
23 33 : cfd_status_t cfd_nn_model_load(const char* path, cfd_nn_model_t** out_model) {
24 33 : if (!path) {
25 : return CFD_ERROR_INVALID;
26 : }
27 33 : return cfd_nn_load_impl(path, NULL, 0, out_model);
28 : }
29 :
30 123 : cfd_status_t cfd_nn_model_load_memory(const void* bytes, size_t size,
31 : cfd_nn_model_t** out_model) {
32 123 : if (!bytes || size == 0) {
33 : return CFD_ERROR_INVALID;
34 : }
35 122 : return cfd_nn_load_impl(NULL, bytes, size, out_model);
36 : }
37 :
38 27 : size_t cfd_nn_model_inputs(const cfd_nn_model_t* model) {
39 27 : return model ? model->inputs : 0;
40 : }
41 :
42 25 : size_t cfd_nn_model_outputs(const cfd_nn_model_t* model) {
43 25 : return model ? model->outputs : 0;
44 : }
45 :
46 3 : const char* cfd_nn_model_name(const cfd_nn_model_t* model) {
47 3 : return (model && model->name) ? model->name : "";
48 : }
49 :
50 : /* ==========================================================================
51 : * Backend selection
52 : * ========================================================================== */
53 :
54 : struct cfd_nn_context {
55 : const cfd_nn_model_t* model;
56 : const cfd_nn_backend_impl_t* impl;
57 : size_t max_batch;
58 : float* buf_a; /* ping-pong activation buffers */
59 : float* buf_b;
60 : size_t buf_elems;
61 : float* scratch; /* impl->scratch_lanes * widest, or NULL */
62 : };
63 :
64 27 : static const cfd_nn_backend_impl_t* resolve_backend(cfd_nn_backend_t want) {
65 27 : switch (want) {
66 : case CFD_NN_BACKEND_SCALAR:
67 : return &cfd_nn_impl_scalar;
68 3 : case CFD_NN_BACKEND_SIMD:
69 3 : return cfd_nn_simd_impl();
70 2 : case CFD_NN_BACKEND_OMP:
71 2 : return cfd_nn_impl_omp.dense ? &cfd_nn_impl_omp : NULL;
72 0 : case CFD_NN_BACKEND_AUTO:
73 : /* Priority SIMD > OMP > scalar, matching BC_BACKEND_AUTO. AUTO is
74 : * allowed to pick; an EXPLICIT request that cannot be honoured is
75 : * an error, never a silent substitution. */
76 0 : if (cfd_nn_simd_impl()) {
77 0 : return cfd_nn_simd_impl();
78 : }
79 0 : if (cfd_nn_impl_omp.dense) {
80 : return &cfd_nn_impl_omp;
81 : }
82 : return &cfd_nn_impl_scalar;
83 : default:
84 : return NULL;
85 : }
86 : }
87 :
88 4 : bool cfd_nn_backend_available(cfd_nn_backend_t backend) {
89 4 : return resolve_backend(backend) != NULL;
90 : }
91 :
92 : /* ==========================================================================
93 : * Context
94 : * ========================================================================== */
95 :
96 24 : cfd_status_t cfd_nn_context_create(const cfd_nn_model_t* model, size_t max_batch,
97 : cfd_nn_backend_t backend,
98 : cfd_nn_context_t** out_ctx) {
99 24 : if (!out_ctx) {
100 : return CFD_ERROR_INVALID;
101 : }
102 24 : *out_ctx = NULL;
103 : /* max_batch is caller-supplied, so both products that size the scratch
104 : * are checked before they are formed: a wrapped element or byte count
105 : * would allocate a short buffer and leave the kernels writing past it. */
106 24 : if (!model || max_batch == 0 || model->widest == 0 ||
107 23 : max_batch > SIZE_MAX / model->widest ||
108 23 : max_batch * model->widest > SIZE_MAX / sizeof(float)) {
109 : return CFD_ERROR_INVALID;
110 : }
111 :
112 23 : const cfd_nn_backend_impl_t* impl = resolve_backend(backend);
113 23 : if (!impl) {
114 : /* No fallback: the caller decides what to do, per the project rule
115 : * that an unavailable backend is reported, never substituted. */
116 : return CFD_ERROR_UNSUPPORTED;
117 : }
118 :
119 22 : cfd_nn_context_t* ctx = (cfd_nn_context_t*)cfd_calloc(1, sizeof(*ctx));
120 22 : if (!ctx) {
121 : return CFD_ERROR_NOMEM;
122 : }
123 22 : ctx->model = model;
124 22 : ctx->impl = impl;
125 22 : ctx->max_batch = max_batch;
126 22 : ctx->buf_elems = max_batch * model->widest;
127 :
128 22 : ctx->buf_a = (float*)cfd_aligned_malloc(ctx->buf_elems * sizeof(float));
129 22 : ctx->buf_b = (float*)cfd_aligned_malloc(ctx->buf_elems * sizeof(float));
130 : /* Sized by the model, not the batch: scratch_lanes is a vector width and
131 : * widest is capped at CFD_NN_MAX_FEATURES, so this product cannot wrap. */
132 22 : if (impl->scratch_lanes) {
133 0 : ctx->scratch = (float*)cfd_aligned_malloc(impl->scratch_lanes * model->widest *
134 : sizeof(float));
135 : }
136 22 : if (!ctx->buf_a || !ctx->buf_b || (impl->scratch_lanes && !ctx->scratch)) {
137 0 : cfd_nn_context_destroy(ctx);
138 0 : return CFD_ERROR_NOMEM;
139 : }
140 22 : *out_ctx = ctx;
141 22 : return CFD_SUCCESS;
142 : }
143 :
144 23 : void cfd_nn_context_destroy(cfd_nn_context_t* ctx) {
145 23 : if (!ctx) {
146 : return;
147 : }
148 22 : cfd_aligned_free(ctx->buf_a);
149 22 : cfd_aligned_free(ctx->buf_b);
150 22 : cfd_aligned_free(ctx->scratch);
151 22 : cfd_free(ctx);
152 : }
153 :
154 3 : const char* cfd_nn_context_backend(const cfd_nn_context_t* ctx) {
155 3 : if (!ctx || !ctx->impl) {
156 : return "none";
157 : }
158 : /* The SIMD tables are per architecture, so this names the arch actually
159 : * selected at runtime and a test cannot pass while running scalar code. */
160 3 : return ctx->impl->name;
161 : }
162 :
163 34 : size_t cfd_nn_context_capacity(const cfd_nn_context_t* ctx) {
164 34 : return ctx ? ctx->max_batch : 0;
165 : }
166 :
167 25 : const cfd_nn_model_t* cfd_nn_context_model(const cfd_nn_context_t* ctx) {
168 25 : return ctx ? ctx->model : NULL;
169 : }
170 :
171 : /* ==========================================================================
172 : * Inference
173 : * ========================================================================== */
174 :
175 101 : cfd_status_t cfd_nn_predict_batch(cfd_nn_context_t* ctx, size_t batch,
176 : const double* input, size_t input_count,
177 : double* output, size_t output_count) {
178 101 : if (!ctx || !input || !output || batch == 0) {
179 : return CFD_ERROR_INVALID;
180 : }
181 101 : const cfd_nn_model_t* m = ctx->model;
182 101 : if (batch > ctx->max_batch) {
183 : return CFD_ERROR_INVALID;
184 : }
185 100 : if (input_count != batch * m->inputs || output_count != batch * m->outputs) {
186 : return CFD_ERROR_INVALID;
187 : }
188 :
189 : /* f64 -> f32 at the boundary; weights and arithmetic are f32. */
190 98 : float* src = ctx->buf_a;
191 98 : float* dst = ctx->buf_b;
192 2831 : for (size_t i = 0; i < input_count; i++) {
193 2733 : src[i] = (float)input[i];
194 : }
195 :
196 207 : for (size_t li = 0; li < m->layer_count; li++) {
197 109 : ctx->impl->dense(&m->layers[li], batch, src, dst, ctx->scratch);
198 109 : float* tmp = src;
199 109 : src = dst;
200 109 : dst = tmp;
201 : }
202 :
203 : /* A corrupt or badly scaled model must fail loudly rather than seed NaN
204 : * into a flow field, so the result is checked before it is handed back. */
205 942 : for (size_t i = 0; i < output_count; i++) {
206 845 : if (!isfinite(src[i])) {
207 : return CFD_ERROR_DIVERGED;
208 : }
209 844 : output[i] = (double)src[i];
210 : }
211 : return CFD_SUCCESS;
212 : }
|