xref: /aosp_15_r20/external/libaom/av1/encoder/ml.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/mathutils.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/ml.h"
18*77c1e3ccSAndroid Build Coastguard Worker 
av1_nn_output_prec_reduce(float * const output,int num_output)19*77c1e3ccSAndroid Build Coastguard Worker void av1_nn_output_prec_reduce(float *const output, int num_output) {
20*77c1e3ccSAndroid Build Coastguard Worker   const int prec_bits = 9;
21*77c1e3ccSAndroid Build Coastguard Worker   const int prec = 1 << prec_bits;
22*77c1e3ccSAndroid Build Coastguard Worker   const float inv_prec = (float)(1.0 / prec);
23*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < num_output; i++) {
24*77c1e3ccSAndroid Build Coastguard Worker     output[i] = ((int)(output[i] * prec + 0.5)) * inv_prec;
25*77c1e3ccSAndroid Build Coastguard Worker   }
26*77c1e3ccSAndroid Build Coastguard Worker }
27*77c1e3ccSAndroid Build Coastguard Worker 
28*77c1e3ccSAndroid Build Coastguard Worker // Calculate prediction based on the given input features and neural net config.
29*77c1e3ccSAndroid Build Coastguard Worker // Assume there are no more than NN_MAX_NODES_PER_LAYER nodes in each hidden
30*77c1e3ccSAndroid Build Coastguard Worker // layer.
av1_nn_predict_c(const float * input_nodes,const NN_CONFIG * const nn_config,int reduce_prec,float * const output)31*77c1e3ccSAndroid Build Coastguard Worker void av1_nn_predict_c(const float *input_nodes,
32*77c1e3ccSAndroid Build Coastguard Worker                       const NN_CONFIG *const nn_config, int reduce_prec,
33*77c1e3ccSAndroid Build Coastguard Worker                       float *const output) {
34*77c1e3ccSAndroid Build Coastguard Worker   int num_input_nodes = nn_config->num_inputs;
35*77c1e3ccSAndroid Build Coastguard Worker   int buf_index = 0;
36*77c1e3ccSAndroid Build Coastguard Worker   float buf[2][NN_MAX_NODES_PER_LAYER];
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker   // Propagate hidden layers.
39*77c1e3ccSAndroid Build Coastguard Worker   const int num_layers = nn_config->num_hidden_layers;
40*77c1e3ccSAndroid Build Coastguard Worker   assert(num_layers <= NN_MAX_HIDDEN_LAYERS);
41*77c1e3ccSAndroid Build Coastguard Worker   for (int layer = 0; layer < num_layers; ++layer) {
42*77c1e3ccSAndroid Build Coastguard Worker     const float *layer_weights = nn_config->weights[layer];
43*77c1e3ccSAndroid Build Coastguard Worker     const float *layer_bias = nn_config->bias[layer];
44*77c1e3ccSAndroid Build Coastguard Worker     float *output_nodes = buf[buf_index];
45*77c1e3ccSAndroid Build Coastguard Worker     const int num_output_nodes = nn_config->num_hidden_nodes[layer];
46*77c1e3ccSAndroid Build Coastguard Worker     assert(num_output_nodes < NN_MAX_NODES_PER_LAYER);
47*77c1e3ccSAndroid Build Coastguard Worker     for (int node = 0; node < num_output_nodes; ++node) {
48*77c1e3ccSAndroid Build Coastguard Worker       float val = layer_bias[node];
49*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < num_input_nodes; ++i)
50*77c1e3ccSAndroid Build Coastguard Worker         val += layer_weights[node * num_input_nodes + i] * input_nodes[i];
51*77c1e3ccSAndroid Build Coastguard Worker       // ReLU as activation function.
52*77c1e3ccSAndroid Build Coastguard Worker       val = val > 0.0f ? val : 0.0f;  // Could use AOMMAX().
53*77c1e3ccSAndroid Build Coastguard Worker       output_nodes[node] = val;
54*77c1e3ccSAndroid Build Coastguard Worker     }
55*77c1e3ccSAndroid Build Coastguard Worker     num_input_nodes = num_output_nodes;
56*77c1e3ccSAndroid Build Coastguard Worker     input_nodes = output_nodes;
57*77c1e3ccSAndroid Build Coastguard Worker     buf_index = 1 - buf_index;
58*77c1e3ccSAndroid Build Coastguard Worker   }
59*77c1e3ccSAndroid Build Coastguard Worker 
60*77c1e3ccSAndroid Build Coastguard Worker   // Final output layer.
61*77c1e3ccSAndroid Build Coastguard Worker   const float *layer_weights = nn_config->weights[num_layers];
62*77c1e3ccSAndroid Build Coastguard Worker   const float *layer_bias = nn_config->bias[num_layers];
63*77c1e3ccSAndroid Build Coastguard Worker   for (int node = 0; node < nn_config->num_outputs; ++node) {
64*77c1e3ccSAndroid Build Coastguard Worker     float val = layer_bias[node];
65*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < num_input_nodes; ++i)
66*77c1e3ccSAndroid Build Coastguard Worker       val += layer_weights[node * num_input_nodes + i] * input_nodes[i];
67*77c1e3ccSAndroid Build Coastguard Worker     output[node] = val;
68*77c1e3ccSAndroid Build Coastguard Worker   }
69*77c1e3ccSAndroid Build Coastguard Worker   if (reduce_prec) av1_nn_output_prec_reduce(output, nn_config->num_outputs);
70*77c1e3ccSAndroid Build Coastguard Worker }
71*77c1e3ccSAndroid Build Coastguard Worker 
72*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_NN_V2
73*77c1e3ccSAndroid Build Coastguard Worker // Applies the ReLu activation to one fc layer
74*77c1e3ccSAndroid Build Coastguard Worker // output[i] = Max(input[i],0.0f)
nn_relu(const float * input,FC_LAYER * layer)75*77c1e3ccSAndroid Build Coastguard Worker static float *nn_relu(const float *input, FC_LAYER *layer) {
76*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < layer->num_outputs; ++i) {
77*77c1e3ccSAndroid Build Coastguard Worker     layer->output[i] = AOMMAX(input[i], 0.0f);
78*77c1e3ccSAndroid Build Coastguard Worker   }
79*77c1e3ccSAndroid Build Coastguard Worker 
80*77c1e3ccSAndroid Build Coastguard Worker   return layer->output;
81*77c1e3ccSAndroid Build Coastguard Worker }
82*77c1e3ccSAndroid Build Coastguard Worker 
83*77c1e3ccSAndroid Build Coastguard Worker // Applies the Sigmoid activation to one fc layer
84*77c1e3ccSAndroid Build Coastguard Worker // output[i] = 1/(1+exp(input[i]))
nn_sigmoid(const float * input,FC_LAYER * layer)85*77c1e3ccSAndroid Build Coastguard Worker static float *nn_sigmoid(const float *input, FC_LAYER *layer) {
86*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < layer->num_outputs; ++i) {
87*77c1e3ccSAndroid Build Coastguard Worker     const float tmp = AOMMIN(AOMMAX(input[i], -10.0f), 10.0f);
88*77c1e3ccSAndroid Build Coastguard Worker     layer->output[i] = 1.0f / (1.0f + expf(-tmp));
89*77c1e3ccSAndroid Build Coastguard Worker   }
90*77c1e3ccSAndroid Build Coastguard Worker 
91*77c1e3ccSAndroid Build Coastguard Worker   return layer->output;
92*77c1e3ccSAndroid Build Coastguard Worker }
93*77c1e3ccSAndroid Build Coastguard Worker 
94*77c1e3ccSAndroid Build Coastguard Worker // Forward prediction in one fc layer, used in function av1_nn_predict_V2
nn_fc_forward(const float * input,FC_LAYER * layer)95*77c1e3ccSAndroid Build Coastguard Worker static float *nn_fc_forward(const float *input, FC_LAYER *layer) {
96*77c1e3ccSAndroid Build Coastguard Worker   const float *weights = layer->weights;
97*77c1e3ccSAndroid Build Coastguard Worker   const float *bias = layer->bias;
98*77c1e3ccSAndroid Build Coastguard Worker   assert(layer->num_outputs < NN_MAX_NODES_PER_LAYER);
99*77c1e3ccSAndroid Build Coastguard Worker   // fc
100*77c1e3ccSAndroid Build Coastguard Worker   for (int node = 0; node < layer->num_outputs; ++node) {
101*77c1e3ccSAndroid Build Coastguard Worker     float val = bias[node];
102*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < layer->num_inputs; ++i) val += weights[i] * input[i];
103*77c1e3ccSAndroid Build Coastguard Worker     layer->output[node] = val;
104*77c1e3ccSAndroid Build Coastguard Worker     weights += layer->num_inputs;
105*77c1e3ccSAndroid Build Coastguard Worker   }
106*77c1e3ccSAndroid Build Coastguard Worker 
107*77c1e3ccSAndroid Build Coastguard Worker   // activation
108*77c1e3ccSAndroid Build Coastguard Worker   switch (layer->activation) {
109*77c1e3ccSAndroid Build Coastguard Worker     case NONE: return layer->output;
110*77c1e3ccSAndroid Build Coastguard Worker     case RELU: return nn_relu(layer->output, layer);
111*77c1e3ccSAndroid Build Coastguard Worker     case SIGMOID: return nn_sigmoid(layer->output, layer);
112*77c1e3ccSAndroid Build Coastguard Worker     case SOFTSIGN:
113*77c1e3ccSAndroid Build Coastguard Worker       assert(0 && "Softsign has not been supported in NN.");  // TO DO
114*77c1e3ccSAndroid Build Coastguard Worker       return NULL;
115*77c1e3ccSAndroid Build Coastguard Worker     default:
116*77c1e3ccSAndroid Build Coastguard Worker       assert(0 && "Unknown activation");  // Unknown activation
117*77c1e3ccSAndroid Build Coastguard Worker       return NULL;
118*77c1e3ccSAndroid Build Coastguard Worker   }
119*77c1e3ccSAndroid Build Coastguard Worker }
120*77c1e3ccSAndroid Build Coastguard Worker 
av1_nn_predict_v2(const float * feature,NN_CONFIG_V2 * nn_config,int reduce_prec,float * output)121*77c1e3ccSAndroid Build Coastguard Worker void av1_nn_predict_v2(const float *feature, NN_CONFIG_V2 *nn_config,
122*77c1e3ccSAndroid Build Coastguard Worker                        int reduce_prec, float *output) {
123*77c1e3ccSAndroid Build Coastguard Worker   const float *input_nodes = feature;
124*77c1e3ccSAndroid Build Coastguard Worker 
125*77c1e3ccSAndroid Build Coastguard Worker   // Propagate the layers.
126*77c1e3ccSAndroid Build Coastguard Worker   const int num_layers = nn_config->num_hidden_layers;
127*77c1e3ccSAndroid Build Coastguard Worker   assert(num_layers <= NN_MAX_HIDDEN_LAYERS);
128*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < num_layers; ++i) {
129*77c1e3ccSAndroid Build Coastguard Worker     input_nodes = nn_fc_forward(input_nodes, nn_config->layer + i);
130*77c1e3ccSAndroid Build Coastguard Worker     assert(nn_config->layer[i + 1].num_inputs ==
131*77c1e3ccSAndroid Build Coastguard Worker            nn_config->layer[i].num_outputs);
132*77c1e3ccSAndroid Build Coastguard Worker   }
133*77c1e3ccSAndroid Build Coastguard Worker 
134*77c1e3ccSAndroid Build Coastguard Worker   // Final layer
135*77c1e3ccSAndroid Build Coastguard Worker   input_nodes = nn_fc_forward(input_nodes, nn_config->layer + num_layers);
136*77c1e3ccSAndroid Build Coastguard Worker   assert(nn_config->layer[num_layers].num_outputs == nn_config->num_logits);
137*77c1e3ccSAndroid Build Coastguard Worker   // Copy the final layer output
138*77c1e3ccSAndroid Build Coastguard Worker   memcpy(output, input_nodes, sizeof(*input_nodes) * nn_config->num_logits);
139*77c1e3ccSAndroid Build Coastguard Worker   if (reduce_prec) av1_nn_output_prec_reduce(output, nn_config->num_logits);
140*77c1e3ccSAndroid Build Coastguard Worker }
141*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_NN_V2
142*77c1e3ccSAndroid Build Coastguard Worker 
av1_nn_softmax(const float * input,float * output,int n)143*77c1e3ccSAndroid Build Coastguard Worker void av1_nn_softmax(const float *input, float *output, int n) {
144*77c1e3ccSAndroid Build Coastguard Worker   // Softmax function is invariant to adding the same constant
145*77c1e3ccSAndroid Build Coastguard Worker   // to all input values, so we subtract the maximum input to avoid
146*77c1e3ccSAndroid Build Coastguard Worker   // possible overflow.
147*77c1e3ccSAndroid Build Coastguard Worker   float max_input = input[0];
148*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 1; i < n; i++) max_input = AOMMAX(max_input, input[i]);
149*77c1e3ccSAndroid Build Coastguard Worker   float sum_out = 0.0f;
150*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < n; i++) {
151*77c1e3ccSAndroid Build Coastguard Worker     // Clamp to range [-10.0, 0.0] to prevent FE_UNDERFLOW errors.
152*77c1e3ccSAndroid Build Coastguard Worker     const float normalized_input = AOMMAX(input[i] - max_input, -10.0f);
153*77c1e3ccSAndroid Build Coastguard Worker     output[i] = expf(normalized_input);
154*77c1e3ccSAndroid Build Coastguard Worker     sum_out += output[i];
155*77c1e3ccSAndroid Build Coastguard Worker   }
156*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < n; i++) output[i] /= sum_out;
157*77c1e3ccSAndroid Build Coastguard Worker }
158*77c1e3ccSAndroid Build Coastguard Worker 
av1_nn_fast_softmax_16_c(const float * input,float * output)159*77c1e3ccSAndroid Build Coastguard Worker void av1_nn_fast_softmax_16_c(const float *input, float *output) {
160*77c1e3ccSAndroid Build Coastguard Worker   const int kNumClasses = 16;
161*77c1e3ccSAndroid Build Coastguard Worker   float max_input = input[0];
162*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 1; i < kNumClasses; i++) max_input = AOMMAX(max_input, input[i]);
163*77c1e3ccSAndroid Build Coastguard Worker   float sum_out = 0.0f;
164*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < kNumClasses; i++) {
165*77c1e3ccSAndroid Build Coastguard Worker     // Clamp to range [-10.0, 0.0] to prevent FE_UNDERFLOW errors.
166*77c1e3ccSAndroid Build Coastguard Worker     const float normalized_input = AOMMAX(input[i] - max_input, -10.0f);
167*77c1e3ccSAndroid Build Coastguard Worker     output[i] = approx_exp(normalized_input);
168*77c1e3ccSAndroid Build Coastguard Worker     sum_out += output[i];
169*77c1e3ccSAndroid Build Coastguard Worker   }
170*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < kNumClasses; i++) output[i] /= sum_out;
171*77c1e3ccSAndroid Build Coastguard Worker }
172