LCOV - code coverage report
Current view: top level - nn/cpu - cfdnn_kernels_scalar.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.0 % 32 24
Test Date: 2026-10-05 04:47:18 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /**
       2              :  * @file cfdnn_kernels_scalar.c
       3              :  * @brief Portable reference kernels for `.cfdnn` inference.
       4              :  *
       5              :  * This is the correctness reference: every other backend must agree with it.
       6              :  * Per the project's scalar-backend policy it is used for validation and small
       7              :  * runs, never for long-running performance tests.
       8              :  *
       9              :  * Accumulation order is part of the contract, not an implementation detail.
      10              :  * The sum over input features runs in ascending index order, and nothing is
      11              :  * ever reduced across samples. SIMD and OpenMP backends parallelise the SAMPLE
      12              :  * axis, which is not reduced, so OpenMP stays bit-identical to this file. SIMD
      13              :  * differs by FMA contraction in the dense sums and by its vector tanh/sigmoid/
      14              :  * softplus approximations (a few ulp; see simd_template/), never by order.
      15              :  */
      16              : 
      17              : #include "../cfdnn_internal.h"
      18              : 
      19              : #include <math.h>
      20              : 
      21         1512 : void cfd_nn_apply_activation(cfd_nn_activation_t act, float param, float* v, size_t n) {
      22         1512 :     switch (act) {
      23              :         case CFD_NN_ACT_IDENTITY:
      24              :             break;
      25              :         case CFD_NN_ACT_RELU:
      26            0 :             for (size_t i = 0; i < n; i++) {
      27            0 :                 if (v[i] < 0.0f) {
      28            0 :                     v[i] = 0.0f;
      29              :                 }
      30              :             }
      31              :             break;
      32              :         case CFD_NN_ACT_LEAKY_RELU:
      33            0 :             for (size_t i = 0; i < n; i++) {
      34            0 :                 if (v[i] < 0.0f) {
      35            0 :                     v[i] *= param;
      36              :                 }
      37              :             }
      38              :             break;
      39              :         case CFD_NN_ACT_TANH:
      40         8806 :             for (size_t i = 0; i < n; i++) {
      41         8288 :                 v[i] = tanhf(v[i]);
      42              :             }
      43              :             break;
      44              :         case CFD_NN_ACT_SIGMOID:
      45            0 :             for (size_t i = 0; i < n; i++) {
      46            0 :                 v[i] = 1.0f / (1.0f + expf(-v[i]));
      47              :             }
      48              :             break;
      49              :         case CFD_NN_ACT_SOFTPLUS:
      50          522 :             for (size_t i = 0; i < n; i++) {
      51              :                 /* log1p(exp(x)) via the max form, which stays finite for large
      52              :                  * x where expf() would overflow. The closure's non-negativity
      53              :                  * guarantee rests on this layer, so it must not produce inf. */
      54          262 :                 float x  = v[i];
      55          262 :                 float mx = x > 0.0f ? x : 0.0f;
      56          262 :                 v[i]     = mx + log1pf(expf(-fabsf(x)));
      57              :             }
      58              :             break;
      59              :         default:
      60              :             break; /* unreachable: the loader rejects unknown activations */
      61              :     }
      62         1512 : }
      63              : 
      64          106 : static void dense_scalar(const cfd_nn_layer_t* l, size_t batch, const float* in,
      65              :                          float* out, float* scratch) {
      66          106 :     (void)scratch;
      67          106 :     const size_t ni = l->in_features;
      68          106 :     const size_t no = l->out_features;
      69              : 
      70         1594 :     for (size_t s = 0; s < batch; s++) {
      71         1488 :         const float* x = in + s * ni;
      72         1488 :         float*       y = out + s * no;
      73        10518 :         for (size_t o = 0; o < no; o++) {
      74         9030 :             const float* w   = l->weights + o * ni; /* row-major [out][in] */
      75         9030 :             float        acc = l->bias ? l->bias[o] : 0.0f;
      76        91411 :             for (size_t i = 0; i < ni; i++) {
      77        82381 :                 acc += x[i] * w[i];
      78              :             }
      79         9030 :             y[o] = acc;
      80              :         }
      81         1488 :         cfd_nn_apply_activation(l->activation, l->act_param, y, no);
      82              :     }
      83          106 : }
      84              : 
      85              : const cfd_nn_backend_impl_t cfd_nn_impl_scalar = {
      86              :     "scalar",
      87              :     dense_scalar,
      88              :     0,
      89              : };
        

Generated by: LCOV version 2.0-1